2012-11-07 49 views
28

我在其中有一个路径的本地应用程序:HttpContext.Current.Request.Url.Host它返回什么?

http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen 

但是当这种去集成环境或者是生产,这将是像

http://www.someshopping.com/m/pages/SearchResults.aspx?search=knife&filter=kitchen 

对于某些情况下,我需要只是通过:

www.someshopping.com 

我的XSLT文件,并在功能之一,我使用这个:

string currentURL = HttpContext.Current.Request.Url.Host; 

这会在本地环境中返回我“localhost”。将相同的代码返回我:

www.someshopping.com生产(我不需要的http://

只是不想采取任何机会。所以问了这个愚蠢的问题。

+3

也许应该是'string host = HttpContext.Current.Request.Url.Host;' – Spike0xff

回答

38

是的,只要你的网址输入到浏览器www.someshopping.com和你不使用URL重写,然后

string currentURL = HttpContext.Current.Request.Url.Host; 

将返回www.someshopping.com

注意当地的区别调试环境和生产环境

+0

它返回'HOSTNAME'头或本地地址,如果头不存在。查看详细信息:https://referencesource.microsoft.com/#System.Web/HttpRequest.cs,36cdd1ccad69cf75 – Alex

15

Host属性将返回您在访问网站时使用的域名。所以,在你的开发环境,因为你的要求

http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen 

它返回localhost。您可以掰开你的网址,像这样:

Protocol: http 
Host: localhost 
Port: 950 
PathAndQuery: /m/pages/SearchResults.aspx?search=knight&filter=kitchen 
+0

所以我的问题是,这将返回生产环境www.someshopping.com? :) –

+0

是的,假设您在制作中请求的URL是www.someshopping.com。 – Tejs

3

试试这个:

string callbackurl = Request.Url.Host != "localhost" 
    ? Request.Url.Host : Request.Url.Authority; 

这将为本地以及生产环境中工作。因为本地使用带有端口号的url,这可能使用Url.Host。

+6

你应该总是使用'Request.IsLocal'来检查它是否是本地请求,不需要比较'Request.Url.Host',因为如果我真的写了'http:// LocalHost/...' – balexandre

相关问题