November Happy Hour will be moved to Thursday December 5th.

Episerver.Forms.ServiceApi and web api attribute routing

Vote:
 

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: http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/9/forms/how-to-use-the-episerver-forms-service-api/

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? 

httpConfig.Routes.Remove("MS_attributerouteWebApi");
httpConfig.MapHttpAttributeRoutes();

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?

#147097
Apr 06, 2016 7:33
Vote:
 

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 

    [ModuleDependency(typeof(EPiServer.Forms.ServiceApi.InitializationModule))]

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

httpConfig.MapHttpAttributeRoutes();

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

#147099
Edited, Apr 06, 2016 9:23
Vote:
 
<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>
#193867
Jun 06, 2018 16:14
Vote:
 

You could try simply adding this to app settings:

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

It worked for some people.

Thanks,

Steve

#193893
Jun 07, 2018 10:39
Vote:
 

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.

#193896
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.