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

17
我正在使用ASP.net。我的网站托管在IIS根目录下的子文件夹test中。所以default.aspx的URL是http://localhost/test/Default.aspx。从default.aspx,我想使用相对路径的Reponse.Redirect()重定向到同一网站内的另一个URLhttp://localhost/test/whatever
我尝试过
Response.Redirect("/whatever");
并且
Response.Redirect("~/whatever");

两者都将重定向到http://localhost/whatever。请注意,Redirect方法使用http://localhost而不是http://localhost/test/作为基础URL。

有什么想法吗?

谢谢。


2
我自己犯了一个愚蠢的错误。Response.Redirect("~/whatever"); 应该跳转到 http://localhost/test/whatever。谢谢大家。 - Shuo
4个回答

18

尝试:

Response.Redirect("hello");

同样也

Response.Redirect("./hello");

享受吧!


不起作用,兄弟。 - user123456

4

抱歉如果我过于简单化或误解了你的问题,但你是否尝试过:

Response.Redirect("hello");

1
问题在于您的应用程序未托管在test目录中,而是托管在其上面的目录中。如果您想让 ~(也称为波浪号)起作用,您需要将test设置为应用程序的虚拟路径,这样您的主机就真正托管在test中,Response.Redirect("~/whatever") 将会起作用。

0

试试这个(我的示例使用VB.net)

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接