URL Rewriting Module for WCF

Your WCF services have the .svc extension. So a request could be http://yourserver/golden.svc/echo/helloworld

With this simple HTTP module for the IIS pipeline you can avoid the .svc extension, as it automagically adds it to redirects requests with no extension specified.

public class ServiceRedirectorHttpModule : IHttpModule  
{    
    public void Init(HttpApplication context)
    {
        const string dummyAuthority = "http://x";
        context.BeginRequest += delegate
        {
            HttpContext ctx = HttpContext.Current;
            string path = ctx.Request
                             .AppRelativeCurrentExecutionFilePath;
            Uri uri = new Uri(dummyAuthority + path.TrimStart('~'));
            if (uri.Segments.Length > 1)
            {
                string service = uri.Segments[1].TrimEnd('/');
                if (service.Length > 1 
                    && Path.GetExtension(service).Length == 0)
                {
                    service = "~/" + Path.ChangeExtension(service, "svc");
                    string parameters = 
                        uri.Segments
                           .Skip(2)
                           .Aggregate(string.Empty, (acc,x) => acc+=x);
                    if (parameters.Length == 0
                        && path.EndsWith("/", StringComparison.Ordinal))
                    {
                        parameters += '/';
                    }
                    ctx.RewritePath(
                        service,
                        parameters,
                        ctx.Request.QueryString.ToString(),
                        true);
                }
            }
        };
    }
    public void Dispose()
    {
    }
}
Google
m@kli.dk @klinkby RSS feed  GitHub