Get Base URL of My Web Application

前端 未结 2 1877
耶瑟儿~
耶瑟儿~ 2021-01-12 16:10

Is it possible to get the base URL of IIS 7 by using Microsoft.Web.Administration.ServerManager ?

Typically this would be:

http://localh         


        
相关标签:
2条回答
  • 2021-01-12 16:31

    For those interested in the usage of Microsoft.Web.Administration.ServerManager, here's some code. Consider that an IIS application my have more than one binding, resulting in more than one URL per web application.

    var siteName = "Default Web Site";
    var appPath = "MyWebApplication";
    
    var serverManager = new ServerManager();
    var site = serverManager.Sites[siteName];
    appPath = appPath.StartsWith("/") ? appPath : "/" + appPath;
    var app = site.Applications[appPath];
    
    var urls = new List<string>();
    
    foreach (var binding in site.Bindings)
    {
        var sb = new StringBuilder();
        sb.Append(binding.Protocol);
        sb.Append("://");
        if (!string.IsNullOrWhiteSpace(binding.Host))
        {
            sb.Append(binding.Host);
        }
        else
        {
            if (Equals(binding.EndPoint.Address, IPAddress.Any))
            {
                sb.Append("localhost");
            }
            else
            {
                sb.Append(binding.EndPoint.Address);
            }
        }
    
        if (binding.EndPoint.Port != 80)
        {
            sb.Append(":");
            sb.Append(binding.EndPoint.Port);
        }
    
        sb.Append(app.Path);
        urls.Add(sb.ToString());
    }
    
    0 讨论(0)
  • 2021-01-12 16:38

    You can use string baseURL = HttpContext.Current.Request.Url.Host.

    0 讨论(0)
提交回复
热议问题