C# HttpContext.Current.Request.Url.Host 返回什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13273312/
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
HttpContext.Current.Request.Url.Host what it returns?
提问by Amin Sayed
I'm having a local application which has a path:
我有一个本地应用程序,它有一个路径:
http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen
but when this goes to integration environment or perhaps the production, it will be something like
但是当这进入集成环境或生产时,它会像
http://www.someshopping.com/m/pages/SearchResults.aspx?search=knife&filter=kitchen
For some case I need to pass just:
对于某些情况,我只需要通过:
www.someshopping.com
to my XSLT file and in one of the function I'm using this:
到我的 XSLT 文件和我正在使用的功能之一:
string currentURL = HttpContext.Current.Request.Url.Host;
this returns me "localhost" in local environment. Will the same code return me:
这会在本地环境中返回“ localhost”。相同的代码会返回我吗:
www.someshopping.comin production (I DO NOT need http://)
www.someshopping.com正在生产中(我不需要http://)
just don't want to take any chance. So asked this silly question.
只是不想冒险。所以问了这个傻问题。
采纳答案by edhurtig
Yes, as long as the url you type into the browser www.someshopping.com and you aren't using url rewriting then
是的,只要您在浏览器中输入的网址 www.someshopping.com 并且您没有使用网址重写
string currentURL = HttpContext.Current.Request.Url.Host;
will return www.someshopping.com
将返回 www.someshopping.com
Note the difference between a local debugging environment and a production environment
注意本地调试环境和生产环境的区别
回答by Tejs
The Hostproperty will return the domain name you used when accessing the site. So, in your development environment, since you're requesting
该Host属性将返回您在访问站点时使用的域名。所以,在你的开发环境中,因为你要求
http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen
It's returning localhost. You can break apart your URL like so:
它回来了localhost。您可以像这样拆分您的网址:
Protocol: http
Host: localhost
Port: 950
PathAndQuery: /m/pages/SearchResults.aspx?search=knight&filter=kitchen
回答by Mahesh
Try this:
尝试这个:
string callbackurl = Request.Url.Host != "localhost"
? Request.Url.Host : Request.Url.Authority;
This will work for local as well as production environment. Because the local uses url with port no that is possible using Url.Host.
这将适用于本地和生产环境。因为本地使用带有端口号的 url,所以使用 Url.Host 是可能的。

