C# Response.Redirect 使用 ~ Path
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31221/
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
Response.Redirect using ~ Path
提问by 1kevgriff
I have a method that where I want to redirect the user back to a login page located at the root of my web application.
我有一个方法,我想将用户重定向回位于我的 Web 应用程序根目录的登录页面。
I'm using the following code:
我正在使用以下代码:
Response.Redirect("~/Login.aspx?ReturnPath=" + Request.Url.ToString());
This doesn't work though. My assumption was that ASP.NET would automatically resolve the URL into the correct path. Normally, I would just use
但这不起作用。我的假设是 ASP.NET 会自动将 URL 解析为正确的路径。通常,我只会使用
Response.Redirect("../Login.aspx?ReturnPath=" + Request.Url.ToString());
but this code is on a master page, and can be executed from any folder level. How do I get around this issue?
但此代码位于母版页上,可以从任何文件夹级别执行。我该如何解决这个问题?
采纳答案by Duncan Smart
I think you need to drop the "~/" and replace it with just "/", I believe / is the root
我认为你需要去掉“~/”并用“/”替换它,我相信/是根
STOP RIGHT THERE!:-) unless you want to hardcode your web app so that it can only be installed at the root of a web site.
停在那儿!:-) 除非您想硬编码您的网络应用程序,以便它只能安装在网站的根目录下。
"~/" isthe correct thing to use, but the reason that your original code didn't work as expected is that ResolveUrl
(which is used internally by Redirect
) tries to first work out if the path you are passing it is an absolute URL (e.g. "**http://server/**foo/bar.htm" as opposed to "foo/bar.htm") - but unfortunately it does this by simply looking for a colon character ':' in the URL you give it. But in this case it finds a colon in the URL you give in the ReturnPath
query string value, which fools it - therefore your '~/' doesn't get resolved.
"~/"是正确的用法,但是您的原始代码没有按预期工作的原因是ResolveUrl
(由 内部使用Redirect
)首先尝试确定您传递的路径是否是绝对 URL(例如“** http://server/**foo/bar.htm”而不是“foo/bar.htm”) - 但不幸的是,它只是通过在您提供的 URL 中查找冒号字符 ':' 来实现的它。但在这种情况下,它会在您在ReturnPath
查询字符串值中给出的 URL 中找到一个冒号,这会愚弄它 - 因此您的 '~/' 没有得到解析。
The fix is that you should be URL-encoding the ReturnPath
value which escapes the problematic ':' along with any other special characters.
解决方法是您应该对ReturnPath
转义有问题的 ':' 以及任何其他特殊字符的值进行 URL 编码。
Response.Redirect("~/Login.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.ToString()));
Additionally, I recommend that you (or anyone) never use Uri.ToString
- because it gives a human-readable, more "friendly" version of the URL - not a necessarily correct one (it unescapes things). Instead use Uri.AbsoluteUri - like so:
此外,我建议您(或任何人)永远不要使用Uri.ToString
- 因为它提供了一个人类可读的、更“友好”的 URL 版本 - 不一定是正确的(它取消转义)。而是使用 Uri.AbsoluteUri - 像这样:
Response.Redirect("~/Login.aspx?ReturnPath=" + Server.UrlEncode(Request.Url.AbsoluteUri));
回答by Marshall
What about using
怎么用
Response.Redirect(String.Format("http://{0}/Login.aspx?ReturnPath={1}", Request.ServerVariables["SERVER_NAME"], Request.Url.ToString()));
回答by Pavan k
you can resolve the URL first Response.Redirect("~/Login.aspx); and add the parameters after it got resolved.
您可以先解析 URL Response.Redirect("~/Login.aspx); 并在解析后添加参数。