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

时间:2010-06-02 18:57:21

标签: asp.net

我正在使用ASP.net。我的网站托管在IIS根目录下的子文件夹test中。所以default.aspx的网址是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 个答案:

答案 0 :(得分:13)

尝试:

Response.Redirect("hello");

Response.Redirect("./hello");

享受!

答案 1 :(得分:3)

很抱歉,如果我过度简化或误解您的问题,但您只是尝试过:

Response.Redirect("hello");

答案 2 :(得分:0)

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

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

我喜欢在某个类的某个类中使用Utils.RedirectRelative(“〜/ SomeDirectory / SomePage.aspx”),但我不知道“良好实践”是怎样的。

相关问题