WebApi Attribute routing is not working with Sitecore 7.5

If you are into WebApi, you were very excited to get attribute routing working on Sitecore 7.2…

you can read about it in several blogs and it works pretty well:

http://kamsar.net/index.php/2014/05/using-web-api-2-attribute-routing-with-sitecore/

http://patrickdelancy.com/2013/08/sitecore-webapi-living-harmony/#.VHXGi4usV1B

Unfortunately in Sitecore 7.5 rev. 141003 attribute routing it is not working anymore…. you can read more about it here

and this is the error that you would get:

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() 

so my advice if, you want to use WebApi with Sitecore 7.5 is to have something like it:

url: api/Sdbother/getid

public class SdbotherController : ApiController
{
   [System.Web.Http.HttpGet]
   public string GetId()
   {
     return "test";
    }
}

make the global.asax inherits from the following class:

public class GlobalExtended : Sitecore.Web.Application
{
   protected void Application_Start(object sender, EventArgs e)
   {
    GlobalConfiguration.Configure(ConfigureRoutes);
    }

  public static void ConfigureRoutes(HttpConfiguration config)
  {
   config.Routes.MapHttpRoute("DefaultApiRoute",
    "api/{controller}/{action}/{id}",
     new { id = RouteParameter.Optional });

    GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    GlobalConfiguration.Configuration.Formatters.Clear();
    GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
   }
}
Advertisements

One thought on “WebApi Attribute routing is not working with Sitecore 7.5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s