问题描述
我需要使用Microsoft.Web.Administration.ServerManager类为网站设置端口的帮助.
I need help setting a port for a website using the Microsoft.Web.Administration.ServerManager class.
首先,我获得了网站:
Site site = this._serverManager.Sites[section.WebsiteName];
然后我尝试根据传入的设置来设置端口:
Then I try to set the port from my settings I passed in:
foreach (Binding b in from binding in site.Bindings.Where(b => b != null && b.EndPoint != null)
select binding)
{
b.EndPoint.Port = Int32.Parse(section.Port);
Console.WriteLine(b.EndPoint.Port);
} this._serverManager.CommitChanges();
我在其中写入了写入行,并且端口从未更改,这是为什么?我已经知道该网站是有效的,因为我在到达这里之前先进行了检查.
I put the writeline in there to check and the port never gets changed why? I already know the website is valid because I check that before I get here.
Binding binding = site.Bindings.CreateElement();
binding.BindingInformation = String.Format("{2}:{0}:{1}", section.Port,b.Host, b.EndPoint.Address);
//b.EndPoint.Port = Int32.Parse(section.Port);
site.Bindings.Add(binding);
我在上面尝试过,并且得到有关GetAttributeValue的COMException.
I tried that above and I get a COMException about GetAttributeValue.
最后我要感谢以下答案:
Finally got it thanks to the answer below I had to do:
b.BindingInformation = String.Format("{2}:{0}:{1}", section.Port, b.Host, b.EndPoint.Address);
推荐答案
您是否提交更改?
这是我在服务器管理应用程序中拥有的代码(从Xml文档中的数据创建):
This is the code I have in my server management app (created from data in a Xml document):
ServerManager manager = new ServerManager();
Site site = manager.Sites[siteName];
foreach (XElement bindingNode in bindingsNode.Elements("Binding")) {
Binding binding = site.Bindings.CreateElement();
binding.BindingInformation = String.Format("{2}:{0}:{1}", bindingNode.Attribute("Port").Value, bindingNode.Value, bindingNode.Attribute("IP").Value);
site.Bindings.Add(binding);
}
manager.CommitChanges();
这篇关于使用ServerManager类设置网站端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!