13

Has anybody set up Web API 2 Attribute Routing with 8.2 or later? I tried the 'official' route, which is to create a custom pipeline handler to the initialize pipeline that does the registration. I've tried to hook it up with a config as well as using webactivator. I've done something similar to this: http://wp-bartbovendeerdtcom.azurewebsites.net/sitecore-8-webapi-v2-mvc-and-attribute-routing/ - In both cases, I get an error

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

I've also used a marketplace module (https://marketplace.sitecore.net/Modules/A/Attribute_Routing_Support.aspx?sc_lang=en), which is essentially the same thing, but hooked up with a config. All same error.

It makes me think that a web api registration is happening somewhere else already in Sitecore registrations, but I'm not sure where. Even if it does, how do I get past this?

Ideas?

1
  • 1
    This error indicates to me that the 'config.MapHttpAttributeRoutes()' method is called multiple times. Did you scaffold your solution from a Visual Studio MVC/Web API template? You might want to double-check that you're not calling this twice - like for instance, once in your global.asax, and once in your web-activator code Oct 14, 2016 at 20:18

1 Answer 1

15

This should actually now work out of the box on Sitecore 8.2+ including Sitecore 9 and 10 with no additional code or configuration required.

Among the resolved issues from the 8.2 release notes:

​Sitecore.Services.Client prevents using Attribute Routing with ASP.NET WebAPI​.

Sitecore is calling the config.MapHttpAttributeRoutes() from Sitecore.Services.Client in the initialize pipeline. This is the one throwing the error you are seeing if you have already called that method in your own startup logic which is run first.

You can configure the assemblies is resolves based on the documentation here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.