Asp.Net URL 的绝对路径

发布于 2024-07-13 20:39:30 字数 560 浏览 5 评论 0原文

为了使 Web 应用程序能够更轻松地与不同服务器上的另一个应用程序共享文件,我在母版页中使用了基本 href 标签。 正如许多人发现的那样,这破坏了 Web 表单路径。 我有一个工作表单适配器类,但不确定如何获取 url 的绝对路径。 目前,我的程序被硬编码为使用类似于以下内容的内容:

HttpContext Context = HttpContext.Current;
value = "http://localhost" + Context.Request.RawUrl;

值得注意的是,我目前正在本地 IIS 服务器上进行测试,因此我尝试使用的很多东西都有一种奇怪的倾向,以便按照绝对路径的顺序进行操作不包括域名(我本地的IIS外部不可见)。 这意味着它不是绝对路径,因此基本 href 会破坏它。

有没有一种好的方法来处理这个问题,使其无需硬编码即可在本地工作,但上传到服务器时也能正常工作? 我宁愿避免任何涉及在服务器端副本上执行不同操作的事情。

是的,我意识到我可以在本地和服务器上使用单独的 web.config 文件来获取此信息,但这很丑陋并且违反了 DRY。

To make it simpler for a webapp to share files with another app on a different server, I'm using a base href tag in my master page. As many people have discovered, this breaks webform paths. I have a working Form Adaptor class but am not sure how to get the absolute path of the url. Currently, my program is hardcoded to use something akin to :

HttpContext Context = HttpContext.Current;
value = "http://localhost" + Context.Request.RawUrl;

It is worth noting that I'm currently testing on my local IIS server, so there's a strange tendency for a lot of things I've tried using in order what the absolute path do not include the domain name (my local IIS is not visible externally). Which means it isn't an absolute path and thus the base href will wreck it.

Is there a good way to handle this such that it will work locally without hardcoding but will also work properly when uploaded to a server? I'd prefer to avoid anything that involves doing something different on the server-side copy.

Yes, I realize I could use separate web.config files locally and on the server to get this information but this is ugly and violates DRY.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

别靠近我心 2024-07-20 20:39:30

我过去用过这个:

// Gets the base url in the following format: 
// "http(s)://domain(:port)/AppPath"
HttpContext.Current.Request.Url.Scheme 
    + "://"
    + HttpContext.Current.Request.Url.Authority 
    + HttpContext.Current.Request.ApplicationPath;

I have used this in the past:

// Gets the base url in the following format: 
// "http(s)://domain(:port)/AppPath"
HttpContext.Current.Request.Url.Scheme 
    + "://"
    + HttpContext.Current.Request.Url.Authority 
    + HttpContext.Current.Request.ApplicationPath;
只有影子陪我不离不弃 2024-07-20 20:39:30

旧帖子,但这是另一种稍微不那么冗长的方法

var baseUri = new Uri(HttpContext.Current.Request.Url, "/");

Old post but here is another slightly less verbose method

var baseUri = new Uri(HttpContext.Current.Request.Url, "/");
咆哮 2024-07-20 20:39:30

使用字符串插值:

string redirectUri = $"{this.Request.Url.Scheme}://{this.Request.Url.Authority}{this.Request.ApplicationPath}account/signedout";

根据上下文用“this”替换“HttpContext”或“HttpContext.Current”。

Using string interpolation:

string redirectUri = $"{this.Request.Url.Scheme}://{this.Request.Url.Authority}{this.Request.ApplicationPath}account/signedout";

Substitute 'this' for 'HttpContext' or 'HttpContext.Current' based on context.

橘味果▽酱 2024-07-20 20:39:30

我使用了以下内容,它对我的​​客户端和服务器都有效。

string surl = string.Format("{0}://{1}",
    HttpContext.Current.Request.Url.Scheme,
    HttpContext.Current.Request.Url.Authority);

I have used following and it worked for me both client and the server.

string surl = string.Format("{0}://{1}",
    HttpContext.Current.Request.Url.Scheme,
    HttpContext.Current.Request.Url.Authority);
逆蝶 2024-07-20 20:39:30

代码 :

string loginUrl = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/") + "Login/Login.aspx?UserName=" + LoggedinUser["UserName"] + "&Password=" + LoggedinUser["Password"];

Code :

string loginUrl = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/") + "Login/Login.aspx?UserName=" + LoggedinUser["UserName"] + "&Password=" + LoggedinUser["Password"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文