Removing .SVC from WCF REST Service

I got a mail asking question, “How could we remove .SVC from a WCF REST Service?

For example, instead of

clip_image001

We need to remove, .svc extension from address of the WCF service.

Let us say, you have a WCF REST Service up and running

http://localhost:58230/Service1.svc/GetMessage

With output

clip_image003

Read here: How to create 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

1. Add a class in WCF Application project.

2. Add the namespace System.Web

3. Implement the class from IHttpModule

clip_image004

4. Before starting of current context take the context path and rewrite the URL.

clip_image006

Full source code for

RemoveSvc.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WcfService7
{
    public class Removesvc : IHttpModule
    {
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            context.BeginRequest += delegate
            {
                HttpContext cxt = 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);
                }
            };
        }
    }
}

5. Now we need to add the HTTP Module in configuration file of WCF Service.

clip_image008

Where, RemoveSvc is name of the class implementing IHttpModule and WcfService7 is the project name (namespace)

Web.Config


<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules>
      <add name ="removesvc" type ="WcfService7.Removesvc,WcfService7"/>
   </modules>      
  </system.webServer
</configuration>


6. Now host the service in IIS. [Read here : Hosting WCF Service in IIS ]

Now when you open WCF REST Service in browser, you can see you are able to call the service without extension ,

http://localhost:4567/Service1/GetMessage

clip_image010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com