开发者

How can I tell whether a controller action was requested by DNS or IP?

On my WebDev server, I'm trying to test whether a controller's action was requested through its raw IP, or by its DNS. I've tried looking at the controller's HttpRequest.Url.HostNameType, but it seems开发者_JAVA技巧 to resolve to a DNS even if I enter my local IP in the browser. Any ideas? Thanks in advance.


That information is passed in the Host header of an HTTP request, so you should be able to access it like this:

var requestedHost = Request.Headers["Host"];

If the request was for an IP address, that IP address string should be returned. Otherwise, it will be whatever hostname they used.


HttpContext.Current.Request.Url.Authority

Gets the Domain Name System (DNS) host name or IP address and the port number for a server.

Uri baseUri = new Uri("http://www.contoso.com:8080/");
Uri myUri = new Uri(baseUri,"shownew.htm?date=today");

Console.WriteLine(myUri.Authority);

Output: www.contoso.com:8080

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜