tags:

views:

997

answers:

3

When I try to rewrite a URL in ASP.NET I'm finding that the URL changes on the user's browser. I'm using WCF REST services and I want to change the way that you access URLs. See the code example below.

I have an HttpModule that is intercepting requests.

public class FormatModule : IHttpModule
{
    #region IHttpModule Members

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void Init(HttpApplication application)
    {
        application.BeginRequest += new EventHandler(application_BeginRequest);
    }

    void application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context.Request.RawUrl.Contains(".pox")) 
            context.RewritePath("~/Lab1Service.svc?format=pox", false);
        else if (context.Request.RawUrl.Contains(".json")) 
            context.RewritePath("~/Lab1Service.svc?format=json", false);
    }

    #endregion
}

The problem occurs when the users visits the URL in their browser.

http://localhost/Lab1Service.svc.pox, instead the URL changes in the browser to http://localhost/Lab1Service.svc?format=pox.

A: 

Hi, I'm using in my rewriter meodule another context, maybe the problem is in this

public void Init(HttpApplication context){application = context;}

initializes HttpApplication context and then rewrite path

application.Context.RewritePath(rewritedUrl, Config.RebasePath);
Jan Remunda
This still didn't work, what am I missing?
Danny G
A: 

I resolved this. It appears that if you don't include the trailing backslash after the .svc extension the URL REDIRECTS instead of REWRITING.

This was my original

context.RewritePath("~/Lab1Service.svc?format=pox", false);

This is the corrected version (notice the slash after the .svc)

context.RewritePath("~/Lab1Service.svc/?format=pox", false);
Danny G
that's weird.. you must have registered any other handler/modul, whis is doing that redirect..
Jan Remunda
That's actually a forward slash.
StriplingWarrior
A: 

It may be more likely that the IIS pipeline is not routing all URLs through the ASP.NET pipeline. So it sees the .pox or .svc extension and just passes it through generic, static file handlers.

Your "fix" actually hides the extension, so it gets routed through the full .NET pipeline.

Larsenal