I'm working in a Helix-ish solution on Sitecore 8.2.
I can't seem to register any WebAPI controllers. The routes are fine. I've dug pretty far into the GlobalConfiguration
and determined that it's not even registering any of my controllers (GlobalConfiguration.Configuration.Services.GetHttpControllerSelector().GetControllerMapping().ToList()
turns up 68 Controllers, none of which are mine)
- The controller ends with Controller
- it extends ApiController
- I've added the registration to the initialize pipeline (
patch:after="*[@type='Sitecore.Mvc.Pipelines.Initialize.InitializeCommandRoute, Sitecore.Speak.Client']"
) - It appears in DI (in case that matters)
- Just in case, I added it as an
<allowedController>
under the<configuration/sitecore/api/services/configuration>
section - The route is valid (it can resolve the route, it just can't find the class)
Config:
<pipelines>
<initialize>
<processor type="MySolution.Foundation.xDB.RegRoute, MySolution.Foundation.xDB" patch:after="*[@type='Sitecore.Mvc.Pipelines.Initialize.InitializeCommandRoute, Sitecore.Speak.Client']" />
</initialize>
</pipelines>
<api>
<services>
<configuration type="Sitecore.Services.Infrastructure.Configuration.ServicesConfiguration, Sitecore.Services.Infrastructure">
<allowedControllers hint="list:AddController">
<allowedController desc="TestApiController">MySolution.Foundation.xDB.Controllers.TestApiController, MySolution.Foundation.xDB</allowedController>
</allowedControllers>
</configuration>
</services>
</api>
Controller
public class TestApiController : ApiController
{
public string Test()
{
return "Success";
}
}
Registration File
public class RegRoute
{
public virtual void Process(PipelineArgs args)
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
public class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "testapi",
routeTemplate: "-/api",
defaults: new { controller = "TestApi", action = "Test" });
}
}
Result
{"Message":"No HTTP resource was found that matches the request URI 'https://mysite.dev/-/api'.","MessageDetail":"No type was found that matches the controller named 'TestApi'."}
Based off the error, it seems it found the route, but can't find the controller.
Sitecore.Cintel.Endpoint.Plumbing.InitializeRoutes, Sitecore.Cintel
? Several of the processors afterInitializeCommandRoute
make big changes to the Web API configuration, so it's possible your changes are being overwritten. If that fixes it, you'll also need to add anHttpGet
attribute to yourTest
action, otherwise you'll get another error about the requested resource not supporting the "GET" HTTP method.Microsoft.Extensions.DependencyInjection
? I've got a solution on a clean install of Sitecore 8.2 Update-6 with your code that returns a different error message than you're seeing due to a missingHttpGet
attribute:Error message received at /-/api is: "Message": "The requested resource does not support http method 'GET'."
. My solution also has 69 controllers registered (the 68 you mentioned + the custom one from your question), which makes me think something else in yourInitialize
pipeline is causing issues.