tags:

views:

2348

answers:

5

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?

+10  A: 

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.

Darin Dimitrov
+1 thanks, exactly what I was after
Ralph Willgoss
+3  A: 

Its easy on IIS 7 - use a URL Rewrite Module

On IIS 6 I found its easiest to use the ISAPI Rewrite module which lets you define a set of regular expressions that map the request Urls to the .svc file...

Eran Kampf
+4  A: 

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

Rick Strahl
+1  A: 

In IIS6 or 7, you can use IIRF, a free rewriting filter. Here's the rule I used:

# Iirf.ini
#

RewriteEngine ON
RewriteLog  c:\inetpub\iirfLogs\iirf-v2.0.services
RewriteLogLevel 3
StatusInquiry  ON  RemoteOk
CondSubstringBackrefFlag *
MaxMatchCount 10

# remove the .svc tag from external URLs
RewriteRule  ^/services/([^/]+)(?<!\.svc)/(.*)$    /services/$1.svc/$2  [L]
Cheeso
+2  A: 

There is also a way to eliminate the physical .svc files altogether. This can be done with a VirtualPathProvider.

See: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/350f2cb6-febd-4978-ae65-f79735d412db

binarycoder