Response.Redirect 如何计算“~/folder1/folder2/some.aspx”的 URL
这听起来可能是一个微不足道的问题,但由于某种原因,事实并非如此。
我不需要重定向,而是
Response.Redirect("~/folder1/folder2/some.aspx")
需要位置,就像它的行为一样
string navigatingUrl = Response.Redirect("~/folder1/folder2/some.aspx")
尝试复制这个,我从这开始,
string navigatingUrl = new Uri(HttpContext.Current.Request.Url,
new Uri("~/folder1/folder2/some.aspx", UriKind.Relative));
而不是生成 "http://www.fullRequestUrl/~/folder1/folder2/some.aspx"
编辑:好吧,我已经找到为什么我绝对讨厌 Microsoft 的 URL API 了。我祝愿编写这行代码的人地狱之火
else if (uriKind == UriKind.Relative)
{
// Here we know that we can create an absolute Uri, but the user has requested
only a relative one
e = GetException(ParsingError.CannotCreateRelative);
}
是什么让某人抛出这样的错误?这个 if 语句完全破坏了 API。
This might sound like a trivial problem but for some reason it is not.
Instead of needing to redirect
Response.Redirect("~/folder1/folder2/some.aspx")
I need the location as if it behaved like
string navigatingUrl = Response.Redirect("~/folder1/folder2/some.aspx")
Trying to replicate this I started with
string navigatingUrl = new Uri(HttpContext.Current.Request.Url,
new Uri("~/folder1/folder2/some.aspx", UriKind.Relative));
This instead generates "http://www.fullRequestUrl/~/folder1/folder2/some.aspx"
Edit: Well I've found out why I absolutely hate the URL API from Microsoft. I wish hellfire to whoever wrote this line of code
else if (uriKind == UriKind.Relative)
{
// Here we know that we can create an absolute Uri, but the user has requested
only a relative one
e = GetException(ParsingError.CannotCreateRelative);
}
What would possess someone to throw an error like that? This single if statement completely destroys the API.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您正在寻找 Control.ResolveUrl() 。通常,您可能会使用 Page 对象上找到的方法(如果您使用的是 WebForms)。
I think you are looking for Control.ResolveUrl(). Typically you would probably use the method found on your Page object (if you are using WebForms).
窃取 获取我的 asp.net 项目中文件的绝对 url 以包含 HTTP// 等以供外部使用? 唯一的绝对方法是:
Stealing from Get absolute url for a file in my asp.net project to include HTTP// etc for use externally? the only absolute way to do this is: