Try our conversational search powered by Generative AI!

Episerver.Forms.ServiceApi and web api attribute routing


We have a project where we are using Web Api attribute routing to create custom functionalities. Now we needed to use Episerver.Forms.ServiceApi to read submitted forms data. I added Episerver.Forms.ServiceApi nuget to project and fallowed the documentation descripted in here:

When I run the application I get an error: A route named 'MS_attributerouteWebApi' is already in the route collection. Route names must be unique.
Parameter name: name

Apparently Episerver.Forms.ServiceApi is calling HttpConfiguration.MapHttpAttributeRoutes() and according the documentation there is no way to disable it. Episerver.Forms.ServiceApi apparently does not have similar configuration like Episerver.ServiceApi has in Web.config file where you can disable the call to HttpConfiguration.MapHttpAttributeRoutes().

So what to do? I removed HttpConfiguration.MapHttpAttributeRoutes() from our application, hoping that call from Episerver.Forms.ServiceApi would register all routes in application. That did not work and I got an error: System.InvalidOperationException: The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code. 

So what next? Mayby I can remove the 'MS_attributerouteWebApi' and call HttpConfiguration.MapHttpAttributeRoutes() from my project? 


Result: This operation is not supported by 'HostedHttpRouteCollection'. HostedHttpRouteCollection is read only collection, so removing routes is supported.

So what next? I have no idea? Am I missing something obvious?

Apr 06, 2016 7:33

UPDATE: This did not work. Public site is working mostly, but administration is not working and Episerver.Forms.ServiceApi is not working.

I found a solution that worked for me. The problem lies in the order that is used to initialize modules. Just adding 


to my module that was doing Web Api related initialization and removing my call to 


I was able to get the application up and running.  I haven't tested everything yet.... but it seem's to work.

Edited, Apr 06, 2016 9:23
<p>so what did work in the end? I am (now two years later) fighting with the same thing, trying to add my customer web api to existing Episerver site. I ended up using InitializationModule also, my api started to work but haven't done any thorough testing yet on CMS etc.</p>
Jun 06, 2018 16:14

You could try simply adding this to app settings:

<add key="episerver:serviceapi:maphttpattributeroutes" value="false" />

It worked for some people.



Jun 07, 2018 10:39

Yeah, saw that also and was experimenting with it also. I just hate fixes that have tag "works for some people..." foot-in-mouth I will test a bit more and see what combination works for me.

Jun 07, 2018 10:43
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.