ASP. NET gets the original address before URL is overridden

  • 2020-06-15 08:01:46
  • OfStack

The common usage scenario is that when we have a page that requires a user to log in to access, we will determine whether the current user is logged in in the code. If not, we will redirect to the login page and pass the current url to the login page through the Url parameter. If URL rewrite is used and the current url is obtained through ES3en.Url.AbsoluteUri, the user will open the rewritten address after logging in. Although this does not affect normal use, from the perspective of user experience and URL Integration 1, we prefer the address before rewriting.

Previously, we had this problem in development and had to either redirect to the login page via js (get the current url via location.href) or manually write the return address in the code.

Now, we have found the solution. We can find the pre-rewrite url in Request.Headers.

1) If the overridden component USES ISAPI_Rewrite, an item of data will be added to Headers when the overridden URL is accessed: Key is ES22en-ES23en-ES24en with the value of the url before overridden.

2) If the overwritten component USES the URL Rewrite module that comes with IIS, then the Key added to Headers is ES32en-ES33en-ES34en.

In this way, we can easily get the url before the rewrite, the sample code is as follows:


if (Request.Headers["X-Rewrite-URL"] != null)
{
    Response.Write("http://" + Request.Url.Host + Request.Headers["X-Rewrite-URL"]);
}
else if (Request.Headers["X-Original-URL"] != null)
{
    Response.Write("http://" + Request.Url.Host + Request.Headers["X-Original-URL"]);
}


Related articles: