Owin:主机名(或ip)和端口

时间:2014-12-26 04:00:04

标签: asp.net owin katana

我有一个像这样的经典owin启动类:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
    }
 }

有没有办法在“配置”功能中获取主机名(或ip)和端口?

感谢。

1 个答案:

答案 0 :(得分:3)

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
         //Get hostname
         string hostName = System.Net.Dns.GetHostName();

        // Get all ip address.
         IPAddress[] ips;
         ips = System.Net.Dns.GetHostAddresses(hostName);
    }
 }

要从IAppBuilder获取所有值,您可以这样做。

public void Configuration(IAppBuilder appBuilder)
        {
            foreach(var item in appBuilder.Properties)
            {
                //Console.WriteLine(item.Key + " - " + item.Value);
                if(item.Key == "host.Addresses")
                {
                    dynamic d = item.Value;
                    var items = (Dictionary<string,object>)d[0];
                    // Here there are four keys for scheme , host , port , path
                    foreach(var add in items)
                    {
                        Console.WriteLine(add.Key + " - " + add.Value);
                    }
                }
            }
        }