RESTFul people want to control the URI of their services. They focus on building a good information architecture that describes the resources their site provides. Historically most web sites described resources in terms of files and paths in the virtual root of the web server. But what if your resources are coming from a database and there are no files to speak of? What is the URI of your service then?
The default behavior of a RESTful web service in WCF is that you have a SVC file like "MyService.svc" and the first segment of your URI (after the host) is the path to the SVC file on your web site. The remaining portion of the URI is the name of your method and a query string arguments for the parameters.
You can partially control this URI by applying a URI template. So for example in this case we could attribute the operation contract with a template like this
This will change the portion of the URI after the .svc extension so our URI will look like this
Nice, but what if you want to eliminate the .svc extension? Well there are several ways you can get this done
- Scott Guthrie has a great blog post about this – Tip/Trick: Url Rewriting with ASP.NET
- Jon Flanders (MVP) blogged about writing an HttpModule – Using WCF WebHttpBinding and WebGet with nicer Urls
- The IIS7 Team Recently Released a URL Rewrite Module
- For IIS 5/6 you will have to build//use an ISAPI filter (See ScottGu’s post for some free ones)
- New WCF 3.5 SP1 features for URI control like compound segments and default values
- A URL Rewrite Rule for the IIS7 URL Rewrite Module
Be sure to check it out and build some RESTful WCF Services with the URI just the way you want it.