C# 一键调用 Request.Url.Host 和 ApplicationPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/689678/
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
Request.Url.Host and ApplicationPath in one call
提问by Marcus L
Is there any way to get HttpContext.Current.Request.Url.Host
and HttpContext.Current.Request.ApplicationPath
in one call?
有没有什么办法让HttpContext.Current.Request.Url.Host
和HttpContext.Current.Request.ApplicationPath
一个电话吗?
Something like "full application url"?
像“完整的应用程序网址”之类的东西?
EDIT: Clarification - what I need is this the part within []:
编辑:澄清 - 我需要的是 [] 中的部分:
http://[www.mysite.com/mywebapp]/Pages/Default.aspx
I ask simply out of curiosity.
我只是出于好奇而问。
EDIT 2: Thanks for all the replies, but none of them were exactly what I was looking for. FYI, I solved the problem this way (but am still interested in knowing if there's a smoother way):
编辑 2:感谢所有回复,但没有一个是我正在寻找的。仅供参考,我以这种方式解决了问题(但仍然有兴趣知道是否有更顺畅的方法):
public string GetWebAppRoot()
{
if(HttpContext.Current.Request.ApplicationPath == "/")
return "http://" + HttpContext.Current.Request.Url.Host;
else
return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}
采纳答案by MartinHN
public static string GetSiteRoot()
{
string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
if (port == null || port == "80" || port == "443")
port = "";
else
port = ":" + port;
string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
if (protocol == null || protocol == "0")
protocol = "http://";
else
protocol = "https://";
string sOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath;
if (sOut.EndsWith("/"))
{
sOut = sOut.Substring(0, sOut.Length - 1);
}
return sOut;
}
回答by veggerby
Check this post:
检查这个帖子:
public static Uri GetBaseUrl(HttpRequest request)
{
Uri contextUri = new Uri(request.Url, request.RawUrl);
UriBuilder realmUri = new UriBuilder(contextUri) { Path = request.ApplicationPath, Query = null, Fragment = null };
return realmUri.Uri;
}
public static string GetAbsoluteUrl(HttpRequest request, string relativeUrl)
{
return new Uri(GetBaseUrl(request), VirtualPathUtility.ToAbsolute(relativeUrl)).AbsoluteUri;
}
If you don't get what you need from GetBaseUrl direcly, is should be possible to do:
如果你没有直接从 GetBaseUrl 得到你需要的东西,应该可以这样做:
GetAbsoluteUrl(HttpContext.Current.Request, "/")
GetAbsoluteUrl(HttpContext.Current.Request, "/")
回答by Raj
HttpContext.Current.Request.Url.AbsoluteUri
回答by Marcus L
Thanks for all the replies, but none of them were exactly what I was looking for. FYI, I solved the problem this way (but am still interested in knowing if there's a smoother way):
感谢所有回复,但没有一个是我正在寻找的。仅供参考,我以这种方式解决了问题(但仍然有兴趣知道是否有更顺畅的方法):
public string GetWebAppRoot()
{
if(HttpContext.Current.Request.ApplicationPath == "/")
return "http://" + HttpContext.Current.Request.Url.Host;
else
return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}
回答by Dan Diplo
What you should really do is:
你真正应该做的是:
return String.Format("{0}://{1}/", Request.Url.Scheme, Request.Url.Host);
That way it works if you are using HTTPS (or some other schema!)
如果您使用 HTTPS(或其他一些架构!)
回答by Samuel
This was not working on my localhost with a port number so made minor modification:
这在带有端口号的本地主机上不起作用,因此进行了小的修改:
private string GetWebAppRoot()
{
string host = (HttpContext.Current.Request.Url.IsDefaultPort) ?
HttpContext.Current.Request.Url.Host :
HttpContext.Current.Request.Url.Authority;
host = String.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, host);
if (HttpContext.Current.Request.ApplicationPath == "/")
return host;
else
return host + HttpContext.Current.Request.ApplicationPath;
}