C# 在同一站点上执行 Response.Redirect 从一个页面到另一个页面的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13496186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to do a Response.Redirect from one page to another on the same site
提问by
I'm sure this has been asked time and time again. And yes I've searched. However, I'm unable to find an example that clearly demonstrates what I'm looking to accomplish.
我敢肯定,这已经被问过一次又一次。是的,我已经搜索过了。但是,我无法找到一个例子来清楚地展示我想要完成的事情。
I'm currently on mywebsite/test.aspx, I want to Redirect to mywebsite/testing.aspx. However I want this redirect to work both live on server and within debug of visual studio. I've tried
我目前在mywebsite/test.aspx,我想重定向到mywebsite/testing.aspx。但是,我希望此重定向在服务器上和 Visual Studio 的调试中都能正常工作。我试过了
Response.Redirect(Request.RawUrl.Replace(Request.RawUrl,"testing.aspx"))
However this replaces the whole thing.
然而,这取代了整个事情。
Hope this makes sense - mywebsite/test.aspxshould redirect to mywebsite/testing.aspx
希望这是有道理的 -mywebsite/test.aspx应该重定向到mywebsite/testing.aspx
采纳答案by jrummell
Use application relative urls. ~/represents the application root path, so it will work for both /and /virtual-directory/.
使用应用程序相对 url。~/表示应用程序根路径,因此它适用于/和/virtual-directory/。
Response.Redirect("~/testing.aspx");
回答by Anri
You can use url relative to web site root.
These urls always start with /which means 'root'
您可以使用相对于网站根目录的 url。这些网址总是以/“root”开头
For Response.Redirectand any other url you know will be processed by server (like url specified in server control) it's better to start url with ~/as root, this will help with virtual directories mess.
对于Response.Redirect您知道将被服务器处理的任何其他 url(如服务器控件中指定的 url),最好以~/root身份启动 url ,这将有助于解决虚拟目录混乱。
回答by Gregor Primar
If your page is on the same directory level you can just use:
如果您的页面位于同一目录级别,则可以使用:
Response.Redirect("testing.aspx", false);
If your page is on application root you can use following command:
如果您的页面位于应用程序根目录,则可以使用以下命令:
Response.Redirect("~/testing.aspx", false);
And finally, if you page is inside sub directory from current page you can use:
最后,如果您的页面位于当前页面的子目录中,您可以使用:
Response.Redirect("MyFolder/testing.aspx", false);

