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

前端之家收集整理的这篇文章主要介绍了如何使用C#在IIS中获取网站的“浏览”URL?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
说,我在IIS中有“站点名称”网站.我可以通过我的C#代码通过 ServerManager类访问它的大部分功能.我似乎无法弄清楚如何获取它的“浏览”URL,就像我在下面的截图中显示的那样?

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

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

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

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

解决方法

右键单击并转到编辑绑定…在主机名下,您实际上可以看到它是哪个域.

要么

单击站点并在右侧的操作选项卡上单击绑定…

获取URL:

  1. HttpContext.Current.Request.Url.AbsoluteUri;
  2. //http://localhost:8080/app1/Default.aspx
  3.  
  4. HttpContext.Current.Request.Url.AbsolutePath;
  5. // /YourSite/app1/Defaul.aspx
  6.  
  7. HttpContext.Current.Request.Url.Host;
  8. // localhost:8080

编辑:

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

  1. using (ServerManager sm = new ServerManager())
  2. {
  3. foreach (Binding b in sm.Sites[HostingEnvironment.ApplicationHost.GetSiteName()].Bindings)
  4. {
  5. // ...
  6. }
  7. }

猜你在找的C#相关文章