Response.Redirect 如何计算“~/folder1/folder2/some.aspx”的 URL

发布于 2024-10-11 04:25:13 字数 880 浏览 5 评论 0原文

这听起来可能是一个微不足道的问题,但由于某种原因,事实并非如此。

我不需要重定向,而是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谎言 2024-10-18 04:25:13

我认为您正在寻找 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).

木有鱼丸 2024-10-18 04:25:13

窃取 获取我的 asp.net 项目中文件的绝对 url 以包含 HTTP// 等以供外部使用? 唯一的绝对方法是:

string url = Request.Url.GetLeftPart(UriPartial.Authority) 
                    + VirtualPathUtility.ToAbsolute(relativePath)

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:

string url = Request.Url.GetLeftPart(UriPartial.Authority) 
                    + VirtualPathUtility.ToAbsolute(relativePath)
别忘他 2024-10-18 04:25:13
Response.Redirect(Page.ResolveUrl("~/folder1/forlder2/some.aspx"), false);
Response.Redirect(Page.ResolveUrl("~/folder1/forlder2/some.aspx"), false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文