2

I just installed Sitecore and created a MVC web application and published the solution to Sitecore installed directory.

Created a simple Page controller and action in MVC, created a Template, Page in Sitecore, and assigned a Controller rendering. It's working fine.

In the same solution, created another controller called "test". From the browser, I want to call the "test" controller without any controller rendering/page in Sitecore. when I call the controller getting a Document not found error. Tried with controller routing as the below code, still getting the same error.

How to call the custom controller without Sitecore controller rendering and without any routeconfig.cs changes?

public class TestController : Controller
{
    [System.Web.Http.Route("api/test")]
    [System.Web.Http.HttpGet]
    public ActionResult Index()
    {
        return View();
    }
}

3 Answers 3

1
  1. 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
         }
     }
    
  2. 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>
}
}
  1. 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
  1. You need to create your method which represent api call :

     [System.Web.Http.HttpGet]
     [Route("getvcard/{itemIdString}")]
     public HttpResponseMessage GetItemInformation(string itemIdString)
    
  2. You need to register the ApiController

    public class Register : IServicesConfigurator
         {
             public void Configure(IServiceCollection serviceCollection)
             {
    
                 serviceCollection.Replace(ServiceDescriptor.Transient(typeof(PersonApiController),
                     typeof(PersonApiController)));
             }
         }
    
  3. 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>
    
  4. To call the api you need to call it in this way

    https://yourhostname/getvarcad/itemid

I hope this will help you.

2
  • Hi Praveen. I need like an API call. without creating a sitecore controller rendering and without sitecore page. without the sitecore controller rendering/sitecore page, it's throwing Not found error.
    – K.G
    Commented Apr 14, 2023 at 18:49
  • I have also update about how to use APIController, I hope that will help you. Commented Apr 14, 2023 at 18:52
1

You can call controller and methods without Sitecore controller rendering and without any routeconfig.cs changes try the below code. It works like an API and you can also call it from Ajax.

Ajax:

$.ajax({
    url: "api/Sitecore/YourController/YourMethod",
    type: "POST",
    data: { itemId: itemId },
    context: this,
    success: function (data) {
        // success logic
    },
    error: function (data) {
        console.log("error", data);
    }
});

Controller method:

[HttpPost]
public JsonResult YourMethod(string itemId)
{
    if (Sitecore.Data.ID.IsID(itemId))
    {
        Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
        if (item != null)
        {
            //your custom logic goes here.
            model.Title = item.Fields["Title"].Value;
        }
    }
    return Json(model);
}

Hope it helps!

0

You can Post to controllers from the client side using the below format.

/api/sitecore/{yourcontroller}/{action}

Sitecore has its own route to manage the action method which is used as API. You can use it by Ajax call from the front end.

Just post your data as request in the data object and consume the URL in the above format. It acts like API.

The Url structure is needed to be able to access controller actions from within a Sitecore solution would be like this.

http://local.mysite.com/api/sitecore/samplecontroller/sampleaction?sampleparam=abc

For more details, you can follow this article.

Ajax action calls in Sitecore MVC

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.