Simple Routing with WCF Web API and MVC 3?

Topics: Web Api
Jul 19, 2011 at 11:42 PM

I’m having routing troubles and I believe it’s based on the WCF MapServiceRoute<T> extension method. When I place the call before my default route in the application, my service works but any calls to controllers that use Html.ActionLink fail. Direct calls to the urls work (e.g. mysite/Home) 

When I place the call after the default route the ActionLinks work but the service gives me a 404.

Here is the default route straight from the MVC 3 app template:

routes.MapRoute(
         "Default"// Route name
         "{controller}/{action}/{id}"// URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

And here is my super generic wire up of the service:


var bldr = HttpHostConfiguration.Create();
routes.MapServiceRoute<MVCSignup.Services.SignupSvc>("SignupSvc", bldr); 
Jul 21, 2011 at 2:30 AM

Thanks to Steve Michelotti for clearing this up form me.

All that was required was to remove my SignupSvc service from the default route. I'm also assigning the route after the default route is added to the route table. Here is the code that works:

routes.MapRoute(
	"Default"// Route name
	"{controller}/{action}/{id}"// URL with parameters
	new { controller = "Home", action = "Index", id = UrlParameter.Optional },  
	new { controller = "^(?!SignupSvc).*" } // Ah HA!
);
var bldr = HttpHostConfiguration.Create();
routes.MapServiceRoute<SignupSvc>("SignupSvc", bldr);
Coordinator
Jul 21, 2011 at 3:05 AM

An alternative is to create a custom derived ServiceRoute. Override GetVirtualData to return null. That will prevent the route from being evaluated for link / uri gen