HttpContext.Current.Request.Url.Host它返回什么?

Ami*_*yed 34 c# asp.net http httpcontext

我有一个本地应用程序有一个路径:

http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen
Run Code Online (Sandbox Code Playgroud)

但是当它进入集成环境或生产时,它就会像

http://www.someshopping.com/m/pages/SearchResults.aspx?search=knife&filter=kitchen
Run Code Online (Sandbox Code Playgroud)

在某些情况下,我需要通过:

www.someshopping.com
Run Code Online (Sandbox Code Playgroud)

到我的XSLT文件和我正在使用的函数之一:

string currentURL = HttpContext.Current.Request.Url.Host;
Run Code Online (Sandbox Code Playgroud)

这会在本地环境中返回" localhost ".相同的代码会回复我:

www.someshopping.com在制作中(我不需要http://)

只是不想冒任何机会.所以问这个愚蠢的问题.

edh*_*tig 41

是的,只要您在浏览器www.someshopping.com中键入的URL并且您没有使用url重写

string currentURL = HttpContext.Current.Request.Url.Host;
Run Code Online (Sandbox Code Playgroud)

将返回 www.someshopping.com

请注意本地调试环境和生产环境之间的区别


Tej*_*ejs 18

Host属性将返回您在访问站点时使用的域名.因此,在您的开发环境中,因为您正在请求

http://localhost:950/m/pages/Searchresults.aspx?search=knife&filter=kitchen
Run Code Online (Sandbox Code Playgroud)

它正在回归localhost.您可以像这样拆分您的网址:

Protocol: http
Host: localhost
Port: 950
PathAndQuery: /m/pages/SearchResults.aspx?search=knight&filter=kitchen
Run Code Online (Sandbox Code Playgroud)


Mah*_*esh 7

试试这个:

string callbackurl = Request.Url.Host != "localhost" 
    ? Request.Url.Host : Request.Url.Authority;
Run Code Online (Sandbox Code Playgroud)

这适用于本地和生产环境.因为本地使用url和端口号是不可能使用Url.Host.

  • 你应该总是使用`Request.IsLocal`来检查它是否是一个本地请求,如果我真的写了`http:// LocalHost/...,则不需要比较`Request.Url.Host`就是假的. (10认同)