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.
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
This is the corrected version (notice the forward slash after the .svc)