在IIS-Manager中,默认网站绑定在端口80上.如何使用c#按网站名称获取端口?我尝试了以下代码:
var lWebsite = "Default Web Site";
var lServerManager = new ServerManager();
var lPort = lServerManager.Sites[lWebsite].Bindings.GetAttributeValue("Port");lPort导致invalid index异常为null。但是赋值var lPort = lServerManager.Sites[lWebsite]有效。
发布于 2014-04-09 12:49:52
当您访问SiteslWebsite.Bindings时,您正在访问一个绑定集合。当您尝试调用GetAttributeValue(“端口”)时,它失败了,因为这没有任何意义--集合本身没有与其关联的端口号,它只是一个集合。
如果希望访问每个绑定所使用的端口号,则需要遍历这些绑定,并询问每个绑定的相关端口号:
var site = lServerManager.Sites[lWebsite];
foreach (var binding in site.Bindings)
{
int port = binding.EndPoint.Port;
// Now you have this binding's port, and can do whatever you want with it.
}值得强调的是,网站可以绑定到多个端口。您谈论的是获得“该”端口,但这并不一定是这样的-例如,一个通过HTTP和HTTPS服务的网站将有两个绑定,通常在端口80和443上。这就是为什么您必须处理绑定集合的原因,即使在您的示例中,该集合可能只包含一个绑定--它仍然是一个集合。
有关更多细节,请查看the Binding class的MSDN文档。请注意,您可能感兴趣的一些内容将涉及访问绑定的EndPoint属性,如上面的示例所示。
https://stackoverflow.com/questions/22911993
复制相似问题