3

I have been trying to get WebApi working with Sitecore 7.5 (I was able to get the same code working with 7.2) I have left in the config the reference to MVC 5.1 and I am getting the following exception when I try to access a route mapped with an attribute:

[RoutePrefix("test/api/Other")]
[Route("{action=Get}")]
public class OtherController : ApiController
{
    [HttpGet]
    public string GetId()
    {
        return "test";
    }
}

Message: "An error has occurred.", ExceptionMessage: "Value cannot be null. Parameter name: key", ExceptionType: "System.ArgumentNullException", StackTrace: " at System.Collections.Generic.Dictionary2.FindEntry(TKey key) at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& value) at Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"

The code that I have in the application start is the following:

protected void Application_Start(object sender, EventArgs e)
{
    GlobalConfiguration.Configure(ConfigureRoutes);
}

public static void ConfigureRoutes(HttpConfiguration config)
{
    GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    GlobalConfiguration.Configuration.Formatters.Clear();
    GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
}

any help would be appreciated....

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
Stelio
  • 561
  • 6
  • 15
  • Have you added the route prefix ("test/api" in this case) to the IgnoreUrlPrefixes setting in Sitecore? – PizzaTheHut Nov 27 '14 at 12:41
  • yes, I have just tried it to add the route prefix to the IngnoreUrlPrefix, and it is still not working... the funny thing is that the same code is working in a clean 7.2 instance, I think that the new DI Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController is getting an Object reference error – Stelio Nov 27 '14 at 14:38
  • I think that the issue it is in the Attribute routing, where is getting and Object reference, the only way to get it working is not using attribute routing but classic routing, I am talking about it in this post: http://sitecorecommerce.wordpress.com/2014/11/30/webapi-attribute-routing-is-not-working-with-sitecore-7-5/ – Stelio Dec 01 '14 at 09:43
  • I have the same problem. Upgraded from Sitecore 7.2 to 7.5 and I get the same error when requesting through web api. – Torben Junker Kjær Feb 20 '15 at 16:26
  • Do you guys know if this was fixed in the latest 7.5 release? – demisx May 16 '15 at 17:25

1 Answers1

0

Starting with Sitecore 7.5 they are replacing the default IHttpControllerSelector with their own NamespaceHttpControllerSelector which doesn't support Attribute Routing.

However, it is possible to workaround this. You have to create your own version of NamespaceHttpControllerSelector and patch it into the initialize pipeline after this one:

Sitecore.Services.Infrastructure.Sitecore.Pipelines.ServicesWebApiInitializer, Sitecore.Services.Infrastructure.Sitecore

I have created both a Sitecore package and a NuGet package to do this depending on what you prefer and what your needs are.

The "Custom" package creates the code in your solution, so you can edit it yourself if you have special needs. The Sitecore package and the standard NuGet package just drops my assembly in the bin folder and creates a config file in App_Config\Include which patches the initialize pipeline.

If you want to look at the code, or read a bit more about the issue, have a look at my GitHub repository.

Søren Kruse
  • 1,160
  • 10
  • 12