Why RewritePath changes the Browser Url?

Martin picture Martin · Sep 30, 2011 · Viewed 11.3k times · Source

I have an ASP.NET 4 HttpModule (see code below). When the url path starts with "/1.0" I want Cassini/IIS to go to MyService.svc. However, I don't want to show "MyService.svc" to the user (i.e. no update to the url in the browser). I want the user to see "www.something.com/1.0".

I was pretty sure that RewriteUrl isn't supposed to change the browser url, but in my case it does. Any idea why?

    public void Init(HttpApplication context)
    {
        context.BeginRequest +=
            delegate
            {
                HttpContext ctx = HttpContext.Current;
                const string BasePath = "~/1.0";
                if (path.StartsWith(BasePath, StringComparison.OrdinalIgnoreCase))
                {
                    ctx.RewritePath("~/MyService.svc", "this/is/a/path", string.Empty, false);
                }
            };
    }

P.S. I cannot use ASP.NET Routing because of the period/dot in the Url (see ASP.NET MVC Route IDs with a period).

Answer

Artem Koshelev picture Artem Koshelev · Sep 30, 2011

Looks like you have the same problem as described here: ASP.NET RewritePath not working as expected / URL in browser changing

Add the trailing slash to the url:

ctx.RewritePath("~/MyService.svc/", "this/is/a/path", string.Empty, false);

Also, I'm not sure if WCF engine would preserve PathInfo for you. Possibly you'll have to pass parameters with the URL as QueryString.