Create a new controller in that .NET MVC solution.
Name the controller as Blogs
and create a new action method List
inside it as shown below. Write code inside the controller to get blog lists.
public class BlogsController : Controller
{
public ActionResult List()
{
BlogList model = new BlogList();
//model = ………..; Perform database operations and retrieve model details
return View(model); //return model details to the view
}
}
Create a corresponding view for this action method. i.e., create List.cshtml under Views/Blogs/
path in the same .NET solution. Sample view file is shown below.
List.cshtml
@model Sitecore.MyMVCSolution.Website.Models.BlogList
@{
if (Model != null)
{
foreach (item in Model.CList)
{
<div class="testrow">
<div class="prod-name-css">@item.Title</div>
<div class="prod-qty-css">@item.Body</div>
</div>
}
}
else
{
<div>
<h4 style="text-align:center;">
There is no blog.
</h4>
</div>
}
}
- Login to Sitecore Content editor. Go to Layouts->Renderings. Right click on Renderings, click on Insert->Controller Rendering. Enter the data values as shown below.
Controller: Blogs, Controller Action: List
Save and publish the item.
Now in CMS select page item to configure presentation detail,
- select page item, go to "Presentation Tab", click detail.
- List item Edit the
default details by clicking 'Edit' link and Under layout select
layout that you want to choose.
If you want to send request for API, you can use APIController and need to follow next steps
To create an apiController you need to follow next steps
1 . Create a class which inherits from ApiController
public class PersonApiController : ApiController
You need to create your method which represent api call :
[System.Web.Http.HttpGet]
[Route("getvcard/{itemIdString}")]
public HttpResponseMessage GetItemInformation(string itemIdString)
You need to register the ApiController
public class Register : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
serviceCollection.Replace(ServiceDescriptor.Transient(typeof(PersonApiController),
typeof(PersonApiController)));
}
}
You need to register in the configuration :
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<services>
<configurator
type="yourNameSpace.ServicesConfigurator,yourNamespace" />
</services>
</sitecore>
</configuration>
To call the api you need to call it in this way
https://yourhostname/getvarcad/itemid
I hope this will help you.