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

使用Response.Redirect()到相对路径

  •  10
  • Shuo  · 技术社区  · 16 年前

    我正在使用ASP.net。我的网站托管在子文件夹中 test 在IIS根目录下。因此default.aspx的url是 http://localhost/test/Default.aspx . 从default.aspx,我想使用 Reponse.Redirect() http://localhost/test/whatever .

    Response.Redirect("/whatever");
    

    Response.Redirect("~/whatever");
    

    两个都重定向到 http://localhost/whatever . 请注意 Redirect http://localhost 而不是 http://localhost/test/

    有什么想法吗?

    谢谢。

    3 回复  |  直到 16 年前
        1
  •  16
  •   Doug    16 年前

    尝试:

    Response.Redirect("hello");
    

    Response.Redirect("./hello");
    

    好好享受!

        2
  •  3
  •   0bj3ct.m3th0d    16 年前

    Response.Redirect("hello");
    
        3
  •  0
  •   bgs264    16 年前

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

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

    我喜欢将Utils.RedirectRelative(“~/SomeDirectory/SomePage.aspx”)放在某个类的某个地方,但我不知道这是什么“好做法”。

    推荐文章