1

Sitecore Version: 9.0.2

Environment: Azure PaaS CD

We are trying to call /api/sitecore/controller/actionname with AJAX. It's working on the CM server but not on the CD server.

I disabled custom errors and removed HTTP error configuration in web.config, but it still redirects to the custom 404 page I've created.

3 Answers 3

5

It was moved to SPEAK routes in Sitecore 9 and unavailable on CD server. You can resolve it by adding this snippet to your routing configuration (if you really need this API route - best practice is to create your own routes for your API calls):

if (ConfigurationManager.AppSettings["role:define"] == "ContentDelivery")
{
   RouteTable.Routes.MapRoute("Sitecore.Speak.Commands", "api/sitecore/{controller}/{action}");
}
5

The /sitecore/api route is a default route added by Sitecore in configs that are specific to the content management aspects of the platform. These configs are not enabled for content delivery. As noted in x3mxray's answer, they can be reenabled, but I wouldn't recommend it.

Instead of reenabling the default routes for other functionality you (probably) don't need on your CD servers, I would recommend mapping a custom route to your method and not relying on the /api/sitecore prefix.

1. Create pipeline processor to register route

using System.Web.Mvc;
using System.Web.Routing;
using Sitecore.Pipelines;

public class RegisterWebApiRoutes
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.MapRoute("CustomApiRoute",
                                   "api/customaction",
                                   new { controller = "ControllerName", action = "ActionName" },
                                   new { httpMethod = new HttpMethodConstraint("GET") }
        );
    }
}

2. Create patch config to patch in your pipeline processor

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
    <sitecore role:require="Standalone OR ContentManagement OR ContentDelivery">
        <pipelines>
            <initialize>
                <processor type="Custom.Pipelines.Initialize.RegisterWebApiRoutes,Custom" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']"/>
            </initialize>
        </pipelines>
    </sitecore>
</configuration>

3. Update your code to reference this new route

Don't forget to update your code to use the new route!

4
  • 1
    your answer is reasonable and obvious. but question is not how to implement your cusom route, question is why "/api/sitecore is not working in CD server"
    – x3mxray
    Jul 4, 2019 at 0:19
  • In my opinion, both answers are missing the mark. This answer is more accurate because it offers a path forward that is more of a best practice and explained well. While @x3mxray's answer is short and answers the immediate question, it offers a hack that shouldn't actually be done. That is why I upvoted this answer. Jul 4, 2019 at 1:06
  • @x3mxray - I added a blurb at the beginning of my answer to hopefully more directly answer the question. Jul 5, 2019 at 12:07
  • 1
    @petenavarra - I added a blurb at the beginning of my answer to hopefully more directly answer the question. Jul 5, 2019 at 12:07
1

I have experienced the same issue on a project which had a custom pipeline for 404 redirects. Can you look for a similar code in your .config files from CD server?

<pipelines>
  <httpRequestBegin>
      <processor type="MyCustomDLL.NotFoundHttpRequestProcessor, MyCustomDLL"
                        patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']">
      </processor>
  </httpRequestBegin>
</pipelines>

404 response is hard-coded on the pipeline:

HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.StatusDescription = "404 File Not Found";

If that is your case, a possible solution would be checking which URL is being requested in your custom pipeline. e.g.:

if (args.RequestUrl.LocalPath.StartsWith("/api/sitecore", StringComparison.InvariantCultureIgnoreCase))
{
    return;
}

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.