WCF rest services inside MVC Projects
Recently I blogged about WCF rest services with no SVC file and no config. in this post I also discussed the pros/CONS of WCF services as compared to using MVC controller actions for Web Services and I made the case that, in your instances, WCF rest services is better than using the MVC infrastructure because WCF provides:
- A more restful API with less work
- A convenient automatic help page to assist consumers of your service
- Automatic format selection (I. e., XML/JSON) depending on HTTP headers
In any case, using both WCF rest services and MVC-style Web services are * both * quite convenient-and you don't have to choose. you can use both. in fact, you can use both inside the same web application project. however, a recent question on stackoverflow into strates that you have to be careful how you set up your routing in order to make WCF rest services work inside MVC applications.
If you have a look at the question on stackoverflow, you'll see the routes were originally set up like this:
1:Routes. ignoreroute ("{Resource}. axd/{* pathinfo }");
2:
3:Routes. Add (NewServiceroute ("Person",NewWebservicehostfactory (),Typeof(Personservice )));
4:
5:Routes. maproute (
6:"Default",// Route name
7:"{Controller}/{action}/{ID }",// URL with Parameters
8:New{Controller ="Home", Action ="Index", Id = urlparameter. optional}// Parameter defaults
9:);
The service route was for the WCF Service (using no *. SVC file) and the last route was the standard MVC route. in most of the cases this works fine. navigation to the root correctly lands on the homecontroller's index method. navigation to "/person/help" correctly goes to the WCF rest help page and the WCF rest service can be invoked just fine. however, the action links were not correct. when formulating a simple action link like this:
1:<%: HTML. actionlink ("Home","Index","Home")%>
The resulting URL being produced was:"/Person? Action = index & controller = home"Which was clearly not correct. it's hitting the first route and thinking it shoshould just put "person" for the first segment. it then cannot match the "controller" or "action" tokens so it just puts them in the query string. it will attempt to go to the service but the URI is incorrect so, if it's a WCF rest service, you'll just get the default help page.
Of course, we know that the * Order * matters for routing so what if we put the MVC route first and the service route second? In this case, we run into a different problem. if the first URI segment is "person" then it will try to match that as the controller name and you'll get a 404 or, if you're using a controller factory, your IOC container won't be able to find a controller named "personcontroller ".
So how can we get the WCF rest service route and the MVC routes to play nice with each other? The simple answer: Use a route contraint:
1:Routes. maproute (
2:"Default",// Route name
3:"{Controller}/{action}/{ID }",// URL with Parameters
4:New{Controller ="Home", Action ="Index", Id = urlparameter. optional },// Parameter defaults
5:New{Controller ="^ (?! Person ).*"}
6:);
7:
8:Routes. Add (NewServiceroute ("Person",NewWebservicehostfactory (),Typeof(Personservice )));
Notice on line #5 above, we add a route constraint so that it will try to use the MVC route first as long as the first segment (where the "controller" token is positioned) is not the "person" string which matches where we want our WCF rest service to be. if it is "person", then it will fall through to the service route and correctly point to our WCF rest service.
One other interesting note when using service routes in MVC applications-Anytime you're going beyond the default MVC routes, I highly recommend you unit test your routes. this can be done very easily with the testhelper that comes with mvccontrib. it makes it trivial to unit test routes like this:
1:[Testmethod]
2:Public VoidDefault_path_should_be_able_to_match_controller ()
3:{
4:"~ /". Route (). shouldmapto
5:}
The unfortunate caveat here is that you cannot use this when you're combining service routes. if you do, you'll get this exception when you initialize your routes at the start of the unit test by invoking the static registerroutes () method:"System. invalidoperationexception: system. invalidoperationexception: 'servicehostingenvironment. exception' cannot be invoked within the current hosting environment. This API requires that the calling application be hosted in IIS or was. "Fortunately, we can still use Phil haack's route Debugger:
With the URL of "/person/23", it correctly matches my service route for my WCF Service.
WCF rest and MVC are both great and they shoshould be used * together * when appropriate.
See also:
Http://codebetter.com/howarddierking/2011/05/09/using-serviceroute-with-existing-mvc-routes/
Http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/01/11/asp-net-mvc-route-constraints.aspx
Google search key: Service Routes MVC WCF