我想知道如何在运行时使用ASP.NET获取当前网站的IIS绑定设置(主机名、端口、IP地址)。
.NET是否提供获取这些信息的任何方法?
编辑:我需要一种方法来获取配置为http和https端口,以便在从http切换到https时重定向到正确的端口,并在从https切换回http时重定向到正确的端口,如果使用的不是80/443端口。 是否有一种无需扩展特权就可以实现此操作的方法?
敬礼
Microsoft.Web.Administration
在没有管理员权限的情况下读取配置文件;您是否需要管理员权限来 写入 配置文件?我找不到任何关于此的文档。 - Paul d'Aoust您可以通过访问IIS元数据库,使用System.DirectoryServices程序集来完成此操作。
例如,您可以枚举所有站点及其包含的属性配置。
将以下引用添加到您的项目中:
using System.DirectoryServices
// Assuming your Server Id is 1, and you are connecting to your local IIS.
DirectoryEntry de = new DirectoryEntry(@"IIS://localhost/W3SVC/1/Root");
foreach (DirectoryEntry entry in de.Children)
{
foreach (PropertyValueCollection property in entry.Properties)
{
Console.WriteLine("Name: {0}, Value {1}",property.PropertyName, property.Value);
}
}
public static IEnumerable<Binding> GetSiteBindings(Site site)
{
BindingCollection bindings = site.Bindings;
if (bindings != null)
{
foreach (Binding binding in bindings)
{
if (binding != null)
{
yield return binding;
}
}
}
yield return null;
}
ServerManager mgr = new ServerManager();
foreach (Site s in mgr.Sites)
{
Response.Write("Site: " + s);
Response.Write("<br/>");
var siteBindings = GetSiteBindings(s);
if (siteBindings != null)
{
foreach (var binding in siteBindings)
{
if (binding != null)
{
var bindingInformation = binding.BindingInformation;
var host = binding.Host;
var endPoint = binding.EndPoint;
Response.Write("Host: " + host + ", BindingInfo: " + bindingInformation + ", EndPoint: " + endPoint);
Response.Write("<br/>");
}
}
}
Response.Write("----------------------------------");
Response.Write("<br/>");
}
使用的命名空间:
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Microsoft.Web.Administration" %>
引用的程序集: Microsoft.Web.Administration
将上述代码放入Sample.aspx中进行测试:<%@ Page Language="C#" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Microsoft.Web.Administration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
ServerManager mgr = new ServerManager();
foreach (Site s in mgr.Sites)
{
Response.Write("Site: " + s);
Response.Write("<br/>");
var siteBindings = GetSiteBindings(s);
if (siteBindings != null)
{
foreach (var binding in siteBindings)
{
if (binding != null)
{
var bindingInformation = binding.BindingInformation;
var host = binding.Host;
var endPoint = binding.EndPoint;
Response.Write("Host: " + host + ", BindingInfo: " + bindingInformation + ", EndPoint: " + endPoint);
Response.Write("<br/>");
}
}
}
Response.Write("----------------------------------");
Response.Write("<br/>");
}
}
public static IEnumerable<Binding> GetSiteBindings(Site site)
{
BindingCollection bindings = site.Bindings;
if (bindings != null)
{
foreach (Binding binding in bindings)
{
if (binding != null)
{
yield return binding;
}
}
}
yield return null;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="HtmlForm" runat="server">
<div>
</div>
</form>
</body>
</html>