【问题标题】:Using Response.Redirect() to a relative path使用 Response.Redirect() 到相对路径
【发布时间】:2010-06-02 18:57:21
【问题描述】:

我正在使用 ASP.net。我的网站托管在 IIS 根目录下的子文件夹 test 中。所以default.aspx的url是http://localhost/test/Default.aspx。在 default.aspx 中,我想使用带有相对路径的 Reponse.Redirect() 重定向到同一网站内的另一个 url,http://localhost/test/whatever

我试过了

Response.Redirect("/whatever");

Response.Redirect("~/whatever");

他们都重定向到http://localhost/whatever。请注意,Redirect 方法使用 http://localhost 而不是 http://localhost/test/ 作为基本 url。

有什么想法吗?

谢谢。

【问题讨论】:

  • 我自己犯了一个愚蠢的错误。 Response.Redirect("~/whatever");应该去localhost/test/whatever。谢谢大家。

标签: asp.net


【解决方案1】:

试试:

Response.Redirect("hello");

还有

Response.Redirect("./hello");

享受吧!

【讨论】:

  • 不工作的兄弟
【解决方案2】:

对不起,如果我过度简化或误解了您的问题,但您是否尝试过:

Response.Redirect("hello");

【讨论】:

    【解决方案3】:

    试试这个(我的例子是 VB.net)

      Dim url As String = "~/SomeDirectory/SomePage.aspx"
      Response.Redirect(url.Replace("~"c, Request.ApplicationPath))
    

    我喜欢在某个地方的课堂上使用 Utils.RedirectRelative("~/SomeDirectory/SomePage.aspx") 但我不知道这是多么“好的做法”。

    【讨论】:

      【解决方案4】:

      问题是您的应用程序没有托管在目录 test 中,而是托管在它上面的目录中。如果您希望 ~ (又名波浪号)工作,您需要测试您的应用程序的虚拟路径,此时您的主机将真正托管在测试中,并且 Response.Redirect("~/whatever") 将工作。

      【讨论】:

        猜你喜欢
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-30
        • 2011-04-02
        • 1970-01-01
        • 2012-01-10
        相关资源
        最近更新 更多