代码之家  ›  专栏  ›  技术社区  ›  CleverPatrick

响应。通过IIS URL重写模块重写URL的网页重定向

  •  3
  • CleverPatrick  · 技术社区  · 16 年前

    我正在使用IIS Url Rewrite 用于重写ASP.NET Web应用程序的URL的模块。正在从以下位置重写URL:

    http://domain/version/page.aspx

    到

    http://domain/company/page.aspx

    当直接使用绝对路径浏览时,它工作得很好。当使用相对路径在应用程序中导航时,问题就出现了。 任何 一种相对路径。

    所有相对路径都重定向到相应的 http://domain/version/page.aspx 而不是 http://domain/COMPANY/page.aspx 它应该会的。

    我通过在global.asax的beginRequest事件中添加这一行解决了许多问题:

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

    本质上,它只是将虚拟路径重新定位到请求的URL。

    但是即使有了这个响应,重定向仍然会重定向到实际路径,而不是虚拟路径。

    所以这个代码: Response.Redirect("~/test.aspx") 将重定向到 domain/Version/test.aspx 而不是 domain/Company/test.aspx

    如何作出响应。重定向到虚拟路径( domain/COMPANY/test.aspx )而不是实际路径( domain/VERSION/test.aspx )

    谢谢。

    1 回复  |  直到 16 年前
        1
  •  2
  •   riwalk    16 年前

    两种选择。您可以这样做:

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

    或

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

    这是使用动态URL的挑战之一,如果要使用动态URL,则必须处理这些挑战。

    也许最好的解决方案是编写一个所有页面都能看到的函数,它可以为您完成这项工作。这样你就可以写:

    MyRedirectFunction("test.aspx")
    

    所有的细节都在幕后。