Using System.Web.Routing with Data Services (OData)
So you like the new OData protocol and the implementation in WCF Data Services… but you hate having a “.SVC” extension in the URI?
How do you get rid of the .svc extension with WCF Data Services?
Simple… Just use the new ServiceRoute class.
For this example, I’m using a project from my MIX10 session Driving Experiences Via Services using the .NET Framework
The sample includes a simple WCF Data Service that returns information about a conference.
As you can see I’ve put the service under the Services folder. To access it you have to browse to https://localhost:62025/Services/Conference.svc/
The default behavior of the URI in this case is that the folder and file name in the web site are used to create the URI. But if you don’t like that with .NET 4 you can just create a route.
Just add a Global.asax file to your site and add the following code
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
DataServiceHostFactory factory = new DataServiceHostFactory();
RouteTable.Routes.Add(
new ServiceRoute("Conference", factory, typeof(Conference)));
}
}
This code creates a route for the URI https://localhost:62025/Conference that will use the DataServiceHostFactory to create our WCF Data Service class which is the type Conference.
Simple, easy and very cool…
Comments
Anonymous
April 05, 2010
Hi Ron, While this changes the root URI of the service that a user can browse to,does it also change the base Uri of the service as represented in the Service document ? The service document will be available at the : http://localhost:62025/Conference uri. Clients normally use the Base uri of the Data Service and append the set name to follow the links to the Entity Sets. PhaniAnonymous
April 06, 2010
@PhaniRajuYN - Yes it does also change the base URI. See this post http://blogs.msdn.com/rjacobs/archive/2010/04/06/wcf-data-services-and-serviceroute.aspx