ASP.NET RewritePath not working as expected / URL changes in browser

When I try to rewrite the URL in ASP.NET, I find that the URL changes in the user's browser. I am using WCF REST services and I want to change the way URLs are accessed. See the sample code below.

I have an HttpModule that intercepts 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 users visit the URL in their browser.

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

+3
source share
3 answers

. , .svc, URL REDIRECTS REWRITING.

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

( .svc)

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

, , IIS URL- ASP.NET. , .pox .svc , .

"" , .NET-.

+3

meodule , ,

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

HttpApplication,

application.Context.RewritePath(rewritedUrl, Config.RebasePath);
0

Source: https://habr.com/ru/post/1708196/


All Articles