Does anybody have any experience with Web API and Sitecore? I've got a custom database for data not managed by Sitecore, but will be presented alongside Sitecore data.
The API calls work fine when called directly, but after publishing with the Sitecore site, the site won't load and gives an error saying:
"The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code."
I've tried adding EnsureInitialized() to the startup code, like it says, but it gives the same error.
My API is not calling any Sitecore data.
I added a RegisterRoutes class to Sitecore.Pipelines and patched Sitecore with RegisterRoutes.config after Sitecore.Pipelines.Loader.EnsureAnonymousUsers
My relevant files are as follows:
WebApiConfig.cs
using System;
using System.Net.Http.Formatting;
using System.Web.Http;
namespace Feature.DataAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// This block of code sets the application to emit json globally by default
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
new RequestHeaderMapping("Accept", "text/html", StringComparison.InvariantCultureIgnoreCase, true,
"application/json"));
}
}
}
Global.asax.cs
using System.Web.Http;
namespace Feature.DataAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
// This block of code is to handle a circular reference error when serializing the json output
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.EnsureInitialized();
}
}
}
RegisterRoutes.cs
using Sitecore.Pipelines;
using System.Web.Http;
using System.Web.Http.Routing;
namespace Feature.DataAPI.Pipelines
{
public class RegisterRoutes
{
public void Process(PipelineArgs args)
{
GlobalConfiguration.Configure(Configure);
}
private static void Configure(HttpConfiguration configuration)
{
configuration.Routes.Add("API", new HttpRoute("sitecore/api/{controller}/{id}"));
}
}
}
RegisterRoutes.config
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor type="Feature.DataAPI.Pipelines.RegisterRoutes,Feature.DataAPI" patch:after="processor[@type='Sitecore.Pipelines.Loader.EnsureAnonymousUsers, Sitecore.Kernel']" />
</initialize>
</pipelines>
</sitecore>
</configuration>
Update:
After following @Andrey's advice, I have made some progress and am now getting this error:
The controller for path '/dataapi/Product/1' was not found or does not implement IController.
So at least Sitecore is finding it now. I read in this blog here that Sitecore is throwing an error because it is expecting IController whereas Web API implements ApiController instead. It suggests modifying the order of registrations in the Application_Start, but I can no longer modify this file in Sitecore 8.2 as stated by Soren. So, do I have to alter where the processor is patched in the RegisterRoutes.config file? If so, where should it be placed instead?