Saturday, December 1, 2012

WCF : A Simple WCF REST (2) - Remove .svc in REST URL

[Articles for WCF REST Services]
1) WCF : A simple WCF REST service example (1)
2) WCF : A Simple WCF REST (2) - Remove .svc in REST URL
3) WCF : A Simple WCF REST (3) - Use WCF REST template
4) Client : WCF : A Simple REST Client using HttpClient 

In previous post, we used a URL containing .svc endpoint like http://localhost/SimpleRest/SimpleRestService.svc/test

In order to remove .svc from REST URL, we can use ASP.NET routing in .NET 4.0 or above.
(1) Assume we use the same Simple REST WCF project as in previous post.
(2) In Web.config, add UrlRoutingModule module and UrlRoutingHandler handler which are required for ASP.NET routing.

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">    
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule,
            System.Web, Version=4.0.0.0, Culture=neutral, 
            PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler" preCondition="integratedMode" 
                verb="*" path="UrlRouting.axd"/>
    </handlers>
  </system.webServer>

(3) In Web.config, set [aspNetCompatibilityEnabled] attribute to true in WCF host environment.

<system.serviceModel>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" 
      aspNetCompatibilityEnabled="true" />
</system.serviceModel>

(4) Add New Item - [Global Application Class]. This will add global.asax.
(5) Add Reference to [System.ServiceModel.Activation.dll] to use ServiceRoute class.
(6) In Global.asax.cs file, add a Routing entry for REST service. With this routing information, .svc can be omitted.

protected void Application_Start(object sender, EventArgs e)
{
   RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), 
                  typeof(SimpleRestService)));
}

(7) Now we do not have to use .svc in REST URL. In order to test new URL, try http://localhost/SimpleRest/test

And here is an example of REST POST without .svc.



(8) By the way, SimpleRest is web application name, if we want to remove that as well, we can create a Site in IIS Manager and publish REST application onto the Site root.
  • In IIS Manager, rightclick on Sites and click [Add Web Site]
  • Specify any site name and local folder path for web service. You can choose port 80, but only one port can be assigned to one IIS worker process at a time. So you might need to change Default Web Site port to other number like 8888.
  • Ensure that the Site uses .NET 4.0 (or matching version) in [Application Pools] of IIS Manager. And change process [Identity] appropriately if needed.

  • In VS, Publsh REST web service to the Site root.
So now we can use more precise URL such as  http://localhost/test , http://localhost/Customer/1.

2 comments:

  1. In your config file you have:

    PublicKeyToken=b03f5f7f11d50a3a"

    Where did this key come from?

    Charles

    ReplyDelete
  2. b03f5f7f11d50a3a is the public key token of Microsoft for System.Web.dll file.
    You can check it from ILDASM or the following sn.exe command.

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0> sn -T System.web.dll

    Microsoft (R) .NET Framework Strong Name Utility Version 4.0.30319.1
    Copyright (c) Microsoft Corporation. All rights reserved.

    Public key token is b03f5f7f11d50a3a

    ReplyDelete