如何使用 C# 在 IIS 中获取网站的“浏览”URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19854257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get "Browse" URL for web site in IIS using C#?
提问by c00000fd
Say, I have the "Site Name" web site in the IIS. I can access most of its functions via the ServerManagerclass 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?
说,我在 IIS 中有“站点名称”网站。我可以通过C# 代码中的ServerManager类访问它的大部分功能。我似乎无法弄清楚如何获取它的“浏览”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.
附注。请注意,我不需要启动网站本身。
回答by Zaki
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)
{
// ...
}
}
回答by PavanKumar Sriramula
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++;
}
}
回答by Ozesh
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);
}
回答by Lex Li
JexusManager is now open source, so you can check its implementation of Binding.ToUri method,
JexusManager 现在是开源的,所以你可以检查它的 Binding.ToUri 方法的实现,
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).
由于微软的 MWA 没有公开 HostName 部分,你必须用等效的东西替换它(因为你是初始化 ServerManager 的人,你应该知道主机名是什么)。