How to Remove .SVC from WCF REST Service

No.of Views4479
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  18 Sep 2010 10:09:27
Tag : WCF , REST Services
In this article you will learn, How to Remove .SVC from WCF REST Service
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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

Image Loading

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 

Image Loading

Read my old article to know about 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

Image Loading

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

Image Loading

Full source code

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. 

Image Loading

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>
          <serviceMetadata httpGetEnabled="true"/>
          
          <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 

Image Loading

hope helps,Thnk you for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top