如何了解运行 ASP.NET 应用程序的服务器的 DNS 名称?
如果我的应用程序 URL 是 http://www.somehost.com/somepath/application.aspx,我想获取字符串“www.somehost.com”
Server、Contex、Session 或 Request 对象是否有一些属性可以实现此目的?
谢谢!
最佳答案
这将为您提供托管网站的服务器的 DNS IP
void GetDNSServerAddress()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in nics)
{
if (ni.OperationalStatus == OperationalStatus.Up)
{
IPAddressCollection ips = ni.GetIPProperties().DnsAddresses;
foreach (System.Net.IPAddress ip in ips)
{
Console.Write(ip.ToString());
}
}
}
}
但是,在写这篇文章时,我刚刚看到了您编辑的帖子,所以我认为这就是您所追求的:
string host = Request.Url.Scheme + "://" + Request.Url.Host;
希望这有帮助!
关于asp.net - 运行 ASP.NET 应用程序的服务器的 DNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/286791/