简体   繁体   English

如何使用 C# 在 IIS 中获取网站的“浏览”URL?

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

Say, I have the "Site Name" web site in the IIS.说,我在 IIS 中有“站点名称”网站。 I can access most of its functions via the ServerManager class from my C# code.我可以通过 C# 代码中的ServerManager类访问它的大部分功能。 What I can't seem to figure out is how to get the "Browse" URL for it, like I showed on the screenshot below?我似乎无法弄清楚如何获取它的“浏览”URL,就像我在下面的屏幕截图中显示的那样?

在此处输入图片说明

If I go to Manage Website -> Browse in the IIS Manager , it will launch the IE with a URL as such:如果我在IIS Manager 中转到 Manage Website -> Browse ,它将使用 URL 启动 IE,如下所示:

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

So I need to get a URL like that.所以我需要得到一个这样的 URL。

PS.附注。 Note that I don't need to launch the site itself.请注意,我不需要启动网站本身。

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++;                    
    }
}

Right click and go to edit bindings... under Host Name you can actually see which domain it is.右键单击并转到edit bindings...Host Name名下,您实际上可以看到它是哪个域。

Or或者

Click the site and on actions tab on right hand side you can click bindings...单击站点并在右侧的操作选项卡上单击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) :要获取站点信息,请尝试使用HostingEnvironment.ApplicationHost.GetSiteName()HostingEnvironment.ApplicationHost.GetSiteID()请参阅以下示例(未经测试):

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

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);
}

JexusManager is now open source, so you can check its implementation of Binding.ToUri method, JexusManager 现在是开源的,所以你可以检查它的 Binding.ToUri 方法的实现,

https://github.com/jexuswebserver/Microsoft.Web.Administration/blob/master/Microsoft.Web.Administration/Binding.cs 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).由于 Microsoft 的 MWA 没有公开 HostName 部分,您必须用等效的内容替换它(因为您是初始化 ServerManager 的人,您应该知道主机名是什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM