How to get “Browse” URL for web site in IIS using C#?

ぃ、小莉子 提交于 2020-02-15 07:06:44

问题


Say, I have the "Site Name" web site in the IIS. I can access most of its functions via the ServerManager class from my C# code. What I can't seem to figure out is how to get the "Browse" URL for it, like I showed on the screenshot below?

If I go to Manage Website -> Browse in the IIS Manager, it will launch the IE with a URL as such:

http://localhost:8080/app1/Default.aspx

So I need to get a URL like that.

PS. Note that I don't need to launch the site itself.


回答1:


Try this:

using (Microsoft.Web.Administration.ServerManager sm = Microsoft.Web.Administration.ServerManager.OpenRemote("localhost"))
{
    int counter = 0;
    string[] ipAddress = new string[10];
    string[] sites = new string[10];
    List<Tuple<string, string>> mylist = new List<Tuple<string, string>>();

    foreach (var site in sm.Sites)
    {
        sites[counter] = site.Name;

        foreach(var bnd in site.Bindings)
            ipAddress[counter] = bnd.EndPoint != null ? 
                bnd.EndPoint.Address.ToString() : String.Empty;

        mylist.Add(Tuple.Create(sites[counter], ipAddress[counter]));
                counter++;                    
    }
}



回答2:


Right click and go to edit bindings... under Host Name you can actually see which domain it is.

Or

Click the site and on actions tab on right hand side you can click bindings...

To Get the URL :

HttpContext.Current.Request.Url.AbsoluteUri;
//http://localhost:8080/app1/Default.aspx

HttpContext.Current.Request.Url.AbsolutePath;
// /YourSite/app1/Defaul.aspx

HttpContext.Current.Request.Url.Host;
// localhost:8080

Edit:

To get site information try using HostingEnvironment.ApplicationHost.GetSiteName() or HostingEnvironment.ApplicationHost.GetSiteID() see below sample(it is not tested) :

using (ServerManager sm = new ServerManager())
{
    foreach (Binding b in sm.Sites[HostingEnvironment.ApplicationHost.GetSiteName()].Bindings)
    {
        // ...
    }     
}



回答3:


This is one way of getting the browse url

ServerManager serverMgr = new ServerManager();
Site site = serverMgr.Sites["YourSiteName"];
List<string[]> urls = new List<string[]>();
foreach (Binding binding in site.Bindings)
{
    string bindingInfo = binding.BindingInformation;
    string subString = bindingInfo.Substring(2, bindingInfo.Length - 2);
    string[] adrs = subString.Split(':');
    adrs[0] = "localhost:" + adrs[0];
    urls.Add(adrs);
}



回答4:


JexusManager is now open source, so you can check its implementation of Binding.ToUri method,

https://github.com/jexuswebserver/Microsoft.Web.Administration/blob/master/Microsoft.Web.Administration/Binding.cs

        internal string ToUri()
        {
            var address = EndPoint.Address.Equals(IPAddress.Any)
                ? Parent.Parent.Parent.Parent.HostName.ExtractName()
                : EndPoint.AddressFamily == AddressFamily.InterNetwork
                    ? EndPoint.Address.ToString()
                    : string.Format("[{0}]", EndPoint.Address);
            return IsDefaultPort
                ? string.Format("{0}://{1}", Protocol, address)
                : string.Format("{0}://{1}:{2}", Protocol, address, EndPoint.Port);
        }

As Microsoft's MWA does not expose the HostName part, you have to replace that with something equivalent (as you are the one who initialize ServerManager, you should know what is the host name).



来源:https://stackoverflow.com/questions/19854257/how-to-get-browse-url-for-web-site-in-iis-using-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!