Attribute Routing now works out of the box in Sitecore 8.2

Attribute Routing now works out of the box in Sitecore 8.2

With the recently released version 8.2 Sitecore has changed how the Sitecore.Services.Client is initialized and it no longer conflicts with Web API Attribute Routing. In fact it is actually already configured for you and you don’t have to do anything (or it will break).

NOTE: If you want to use Attribute Routing in Sitecore 7.5-8.1 then take a look here.

Normally you would setup Attribute Routing during startup like this:

config.MapHttpAttributeRoutes();

But you shouldn’t do that; you will get an error if you do, when you then try to access your API:

A route named 'MS_attributerouteWebApi' is already in the route collection

This error is probably pretty self explanatory, but it’s because Sitecore does the same thing in the initialize pipeline and it tries to create a route with the same name, which is not allowed.