Reading time: 2 min read
How to Register Custom Routes in Sitecore 9 Helix
Or, how to fix "The object has not yet been initialized. Ensure that HttpConfiguration EnsureInitialized() is called"
Start typing to search...
Or, how to fix "The object has not yet been initialized. Ensure that HttpConfiguration EnsureInitialized() is called"
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor type="Fishtank.Foundation.Feature.RegisterFeatureRoute, Fishtank.Foundation.Feature" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
</initialize>
</pipelines>
</sitecore>
</configuration>
using System.Web.Http;
using System.Web.Routing;
using Sitecore.Pipelines;
namespace Fishtank.Foundation.Feature
{
public class RegisterFeatuerRoute
{
public virtual void Process(PipelineArgs args)
{
RegisterRoute(RouteTable.Routes);
}
protected virtual void RegisterRoute(RouteCollection routes)
{
RouteTable.Routes.MapHttpRoute("routeName",
"your-route-here", /* do not include a forward slash in front of the route */
new {controller = "SiteMap", action = "Generate"} /* controller name should not have the "Controller" suffix */
);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using Sitecore.Data.Items;
using Sitecore.Links;
using Sitecore.Mvc.Extensions;
using Sitecore.Services.Infrastructure.Web.Http;
using Sitecore.Sites;
namespace Fishtank.Foundation.Feature.Controllers
{
// If you want to take advantage of Sitecore security, use the ServicesApiController. If you want to use the .NET controller, use ApiController
public class FeatureController : ServicesApiController
{
[HttpGet]
public IHttpActionResult Generate()
{
var resp = new HttpResponseMessage
{
Content = new StringContent("Some Content", Encoding.UTF8, "text/xml")
};
// Your code here
return ResponseMessage(resp);
}
}
}
Consider the difference between the `ServicesApiController` and the `ApiController` class inheritance. For many simple APIs, the Sitecore `ServicesApiController` class wrapper will cause issues in distributed environments with CM and CD servers. If you want a controller that will work without extra considerations and configurations, use the standard .NET `ApiController` while noting the possible security issues that may arise.
Keep on coding,
Marcel