Tuesday, October 05, 2010

Removing .SVC from WCF REST Service

When we call a service , we call it using the extension of the service. For example , if we are calling a WCF service in the service URL .SVC extension is visible. If we want to remove this extension from the service URL , we will use HTTP Module. This article is going to show how we can achieve that.
I got a mail asking question, “How could we remove .SVC from a WCF REST Service? “
To remove service extension, we will write a HTTP Module and remove the extension and rewrite the path.
Now to remove .svc follows below steps
Add a class in WCF Application project.
Add the namespace System.Web
Implement the class from IHttpModule
publicclassRemovesvc : IHttpModule
Before starting of current context take the context path and rewrite the URL.
context.BeginRequest += delegate
{
   HttpContextcxt = HttpContext.Current;
   string path = cxt.Request.AppRelativeCurrentExecutionFilePath;
   int i = path.IndexOf('/', 2);
   if (i > 0)
   {
       string a = path.Substring(0, i) + ".svc";
       string b = path.Substring(i, path.Length - i);
       string c = cxt.Request.QueryString.ToString();
       cxt.RewritePath(a, b, c, false);
   }
};
Read more: Beyond Relational