Every time somebody tries to access a route "below" our custom API routes, we see an HTTP 500 error. Such errors are quite messy in our log, and we would like to convert it into 404 errors instead, but don't know how.

We have registered custom API routes through the following code:

public void Process(PipelineArgs args)
        RouteTable.Routes.MapRoute("airshoppen-api", "api/{controller}/{action}");

The routing works just fine when we access a controller which exists (e.g. /api/user/), but fails with an exception (and thereby a 500 error) when not (e.g. /api/nosuchcontroller).

The routes have been registered through the following pipeline:

 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <processor type="Website.Feature.Routing.Pipelines.RegisterApiRoutes, Website.Project.Common" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />

Any tips on how to ensure that we don't get spammed with 500 errors?

Note: The error in our log is:

   at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController (Sitecore.Mvc, Version=, Culture=neutral, PublicKeyToken=null)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit (System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest (System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35)
   at System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute (System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Web.HttpApplication+<>c__DisplayClass285_0.<ExecuteStepImpl>b__0 (System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Web.HttpApplication.ExecuteStepImpl (System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Web.HttpApplication.ExecuteStep (System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
Inner exception Sitecore.Mvc.Diagnostics.ExceptionWrapper handled at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController:

2 Answers 2


You can:

Now it should not return any exception and allow Sitecore default functionality to proceed and show 404 page instead of 500.

  • You definitely sent us in the right direction. We had to create an empty default controller as well. With that addition our solution now returns a 404 when no controllers can be found.
    – pwind
    Sep 4, 2023 at 6:44

We followed @marek-musielak's advice but had to expand it a bit.

We added the following Controller Factory:

public class ControllerFactory : SitecoreControllerFactory
        public ControllerFactory(IControllerFactory innerFactory) : base(innerFactory) { }

        public override IController CreateController(RequestContext requestContext, string controllerName)
                return base.CreateController(requestContext, controllerName);
            catch (ControllerCreationException)
                return new DefaultController();

        protected class DefaultController : Controller { }

And registered it using:

public class InitializeControllerFactory
        private readonly Func<ControllerBuilder> _controllerBuilder = () => ControllerBuilder.Current;

        public virtual void Process(PipelineArgs args) => SetControllerFactory(args);

        protected virtual void SetControllerFactory(PipelineArgs args)
            var controllerBuilder = _controllerBuilder();
            SitecoreControllerFactory controllerFactory = new ControllerFactory(controllerBuilder.GetControllerFactory());

Now our solution always returns 404 when no controllers are found.

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.