简体   繁体   English

URL的Asp.Net绝对路径

[英]Asp.Net Absolute Path of a URL

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.为了让 web 应用程序更简单地与不同服务器上的另一个应用程序共享文件,我在我的母版页中使用了一个基本的 href 标签。 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.我有一个可用的 Form Adapter 类,但不确定如何获取 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).值得注意的是,我目前正在我的本地 IIS 服务器上进行测试,所以我尝试使用的很多东西都有一个奇怪的趋势,以便绝对路径不包含域名(我的本地 IIS 不可见)外部)。 Which means it isn't an absolute path and thus the base href will wreck it.这意味着它不是绝对路径,因此 base href 会破坏它。

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.config 文件来获取这些信息,但这很丑陋并且违反了 DRY。

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;

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

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

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);

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.根据上下文将 'this' 替换为 'HttpContext' 或 'HttpContext.Current'。

代码 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM