Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the ".svc" extension in RESTful WCF service?

Tags:

rest

wcf

In my knowledge, the RESTful WCF still has ".svc" in its URL.

For example, if the service interface is like

[OperationContract]
[WebGet(UriTemplate = "/Value/{value}")]
string GetDataStr(string value);

The access URI is like "http://machinename/Service.svc/Value/2". In my understanding, part of REST advantage is that it can hide the implementation details. A RESTful URI like "http://machinename/Service/value/2" can be implemented by any RESTful framework, but a "http://machinename/Service.svc/value/2" exposes its implementation is WCF.

How can I remove this ".svc" host in the access URI?

like image 756
Morgan Cheng Avatar asked Dec 10 '08 05:12

Morgan Cheng


3 Answers

I know this post is a bit old now, but if you happen to be using .NET 4, you should look at using URL Routing (introduced in MVC, but brought into core ASP.NET).

In your app start (global.asax), just have the following route configuration line to setup the default route:

RouteTable.Routes.Add(new ServiceRoute("mysvc", new WebServiceHostFactory(), typeof(MyServiceClass)));

then your URLs would look like this:

http://servername/mysvc/value/2

HTH

like image 145
Thiago Silva Avatar answered Oct 16 '22 22:10

Thiago Silva


In IIS 7 you can use the Url Rewrite Module as explained in this blog post.

In IIS 6 you could write an http module that will rewrite the url:

public class RestModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication app)
    {
        app.BeginRequest += delegate
        {
            HttpContext ctx = HttpContext.Current;
            string path = ctx.Request.AppRelativeCurrentExecutionFilePath;

            int i = path.IndexOf('/', 2);
            if (i > 0)
            {
                string svc = path.Substring(0, i) + ".svc";
                string rest = path.Substring(i, path.Length - i);
                ctx.RewritePath(svc, rest, ctx.Request.QueryString.ToString(), false);
            }
        };
    }
}

And there's a nice example how to achieve extensionless urls in IIS 6 without using third party ISAPI modules or wildcard mapping.

like image 20
Darin Dimitrov Avatar answered Oct 16 '22 22:10

Darin Dimitrov


Here's more detailed info using the IIS 7 Rewrite Module, or using a custom module: http://www.west-wind.com/Weblog/posts/570695.aspx

like image 4
Rick Strahl Avatar answered Oct 16 '22 20:10

Rick Strahl