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

c00*_*0fd 8 c# asp.net iis iis-7

说,我在IIS中有"站点名称"网站.我可以通过C#代码中的ServerManager类访问其大部分功能.我似乎无法弄清楚如何获取它的"浏览"URL,就像我在下面的截图中显示的那样?

在此输入图像描述

如果我在IIS管理器中进入管理网站 - >浏览,它将使用URL启动IE:

http://localhost:8080/app1/Default.aspx
Run Code Online (Sandbox Code Playgroud)

所以我需要获得这样的URL.

PS.请注意,我不需要启动网站本身.

Zak*_*aki 1

右键单击并转到edit bindings...下面,Host Name您实际上可以看到它是哪个域。

或者

单击该站点,然后在右侧的“操作”选项卡上,您可以单击bindings...

获取网址:

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
Run Code Online (Sandbox Code Playgroud)

编辑:

要获取站点信息,请尝试使用HostingEnvironment.ApplicationHost.GetSiteName()HostingEnvironment.ApplicationHost.GetSiteID()查看下面的示例(未经测试):

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