Configure your WCF web service to omit the ".svc" on its URLs

So, you have your fancy dancy .NET WCF service all completed and working nicely. But...it has that ugly ".svc" in the URL:

http://YourSite.com/YourFancyWebService.svc/GetStuff

Yeah...that looks gross. Let's make it prettier:

http://YourSite.com/YourFancyWebService/GetStuff

If you've used the Visual Studio template for creating your WCF service, you'll no doubt have a YourFancyWebService.svc file that has a nested YourFancyWebService.svc.cs file. Similarly, you'll also have a (an?) IYourFancyWebService.cs, which defines your service contract.

  1. Copy the contents of your YourFancyWebService.svc.cs file to the clipboard or a temporary text file.
  2. Delete the YourFancyWebService.svc node in Visual Studio. This should also delete the YourFancyWebService.svc.cs file.
  3. Create a new class file from scratch, naming it YourFancyWebService.cs.
  4. Paste the copied contents of YourFancyWebService.svc.cs into the newly created YourFancyWebService.cs.
  5. Add a new Global Application Class file (Global.asax). This will also add a Global.asax.cs.
  6. In Global.asax.cs, add a new private method:

    private void RegisterRoutes() { RouteTable.Routes.Add(new ServiceRoute("YourFancyWebService", new WebServiceHostFactory(), typeof(YourFancyWebService))); }

  7. In the existing Application_Start method, add a call to the new RegisterRoutes method:

    protected void Application_Start(object sender, EventArgs e) { RegisterRoutes(); }

  8. Make sure to add a reference to both the System.ServiceModel.Activation and System.ServiceModel.Web assemblies. Otherwise,Visual Studio will bark at you about not knowing what WebServiceHostFactory is!

That's it! You should now be able to access your all your WCF web service endpoints at http://YourSite.com/YourFancyWebService instead of http://YourSite.com/YourFancyWebService.svc.

comments powered by Disqus