MVC Attribute Routing and WebApi Attribute Routing for Sitecore 7.5

I am trying to get WebApi

working with Sitecore 7.5 (I managed to get the same code that works with 7.2 ) I left a link in the config MVC 5.1

and also I get the following exception when I try to access the route associated with the attribute:

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

      

Message: "An error has occurred." ExceptionMessage: "The value cannot be zero. Parameter name: key", ExceptionType: "System.ArgumentNullException", StackTrace: "at System.Collections.Generic.Dictionary 2.FindEntry(TKey key) at System.Collections.Generic.Dictionary

2.TryGetValue (TKey, TValue & value) at Sitecore.Services. Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController (HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore (HttpRequestMessage booking request, cancellation) System.Web. ) "

The code I have in starting the application 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 ....

+3


source to share


1 answer


Starting with Sitecore 7.5, they replace the default IHttpControllerSelector

with their own NamespaceHttpControllerSelector

, which does not support attribute routing.

However, this can be circumvented. You have to create your own version NamespaceHttpControllerSelector

and insert it into the pipeline initialize

after that:

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

      

I created both a Sitecore package and a NuGet package for this, depending on which you prefer and what your needs are.



The Custom package generates code in your solution, so you can edit it yourself if you have special needs. Sitecore package and standard NuGet package just dump my build in bin folder and create a config file in App_Config\Include

that fixes the pipeline initialize

.

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

0


source







All Articles