Response.Redirect on Page with Url Rewritten by II

2019-07-04 11:01发布

问题:

I am using the IIS Url Rewrite module to rewrite urls for my ASP.Net web application. The Urls are being rewritten from:

http://domain/version/page.aspx

to

http://domain/company/page.aspx

And it works just fine when browsing directly using absolute paths. The problem comes in when navigating within the app using relative paths. any kind of relative path.

All relative paths redirect to the corresponding http://domain/version/page.aspx instead of to the http://domain/COMPANY/page.aspx that it should be going to.

I solved a lot of the issue by adding this line to the BeginRequest event in Global.asax:

Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
    HttpContext.Current.RewritePath(HttpContext.Current.Request.Url.AbsolutePath, True)
End Sub

Essentially it is just rebasing the virtual path to the requested url.

But even with that Response.Redirect STILL redirects to the actual path and not the virtual path.

So this code: Response.Redirect("~/test.aspx") will redirect to domain/Version/test.aspx instead of domain/Company/test.aspx

How do I make Response.Redirect redirect to the virtual path (domain/COMPANY/test.aspx) instead of to the actual path (domain/VERSION/test.aspx)

Thanks.

回答1:

Two options. You can either do:

Response.Redirect("/company/test.aspx")

or

Response.Redirect("http://domain/company/test.aspx")

This is one of the challenges of using dynamic URLS, and one that you have to deal with if you are going to use them.

Probably the best solution is to write a function that all of your pages can see that does this for you. That way you can just write:

MyRedirectFunction("test.aspx")

and all the details are behind the scenes.