我正在使用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/
作为基本网址。
有什么想法吗?
感谢。
答案 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”),但我不知道“良好实践”是怎样的。