使用 C# 以编程方式在 IIS 中创建网站并设置端口号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1286831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically create a web site in IIS using C# and set port number
提问by Shiraz Bhaiji
We have been able to create a web site. We did this using the information in this link:
我们已经能够创建一个网站。我们使用此链接中的信息执行此操作:
https://msdn.microsoft.com/en-us/library/ms525598.aspx
https://msdn.microsoft.com/en-us/library/ms525598.aspx
However, we would like to use a port number other that port 80. How do we do this?
但是,我们想使用端口 80 以外的端口号。我们如何做到这一点?
We are using IIS 6
我们正在使用 IIS 6
采纳答案by kitsune
If you're using IIS 7, there is a new managed API called Microsoft.Web.Administration
如果您使用的是 IIS 7,则有一个名为Microsoft.Web.Administration的新托管 API
An example from the above blog post:
上面博客文章中的一个例子:
ServerManager iisManager = new ServerManager();
iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\MySite");
iisManager.CommitChanges();
If you're using IIS 6 and want to do this, it's more complex unfortunately.
如果您使用的是 IIS 6 并且想要这样做,不幸的是它会更加复杂。
You will have to create a web service on every server, a web service that handles the creation of a website because direct user impersonation over the network won't work properly (If I recall this correctly).
您必须在每台服务器上创建一个 Web 服务,一个处理网站创建的 Web 服务,因为通过网络直接模拟用户将无法正常工作(如果我没记错的话)。
You will have to use Interop Services and do something similar to this (This example uses two objects, server and site, which are instances of custom classes that store a server's and site's configuration):
您将不得不使用 Interop Services 并执行与此类似的操作(此示例使用两个对象,服务器和站点,它们是存储服务器和站点配置的自定义类的实例):
string metabasePath = "IIS://" + server.ComputerName + "/W3SVC";
DirectoryEntry w3svc = new DirectoryEntry(metabasePath, server.Username, server.Password);
string serverBindings = ":80:" + site.HostName;
string homeDirectory = server.WWWRootPath + "\" + site.FolderName;
object[] newSite = new object[] { site.Name, new object[] { serverBindings }, homeDirectory };
object websiteId = (object)w3svc.Invoke("CreateNewSite", newSite);
// Returns the Website ID from the Metabase
int id = (int)websiteId;
See more here
在这里查看更多
回答by Dewfy
- In properties of site select "Web Site" tab and specify TCP Port.
- In studio to debug purpose specify /
http://localhost:<port>/<site> ; at tab Web for "Use Local IIS Web Server"
- 在站点属性中选择“网站”选项卡并指定 TCP 端口。
- 在工作室调试目的指定 /
http://localhost:<port>/<site> ; 在“使用本地 IIS Web 服务器”的选项卡 Web
回答by Hari
Try the following Code to Know the unUsed PortNo
尝试以下代码以了解未使用的端口号
DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
// Find unused ID PortNo for new web site
bool found_valid_port_no = false;
int random_port_no = 1;
do
{
bool regenerate_port_no = false;
System.Random random_generator = new Random();
random_port_no = random_generator.Next(9000,15000);
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
int site_id = Convert.ToInt32(e.Name);
//For each detected ID find the port Number
DirectoryEntry vRoot = new DirectoryEntry("IIS://localhost/W3SVC/" + site_id);
PropertyValueCollection pvcServerBindings = vRoot.Properties["serverbindings"];
String bindings = pvcServerBindings.Value.ToString().Replace(":", "");
int port_no = Convert.ToInt32(bindings);
if (port_no == random_port_no)
{
regenerate_port_no = true;
break;
}
}
}
found_valid_port_no = !regenerate_port_no;
} while (!found_valid_port_no);
int newportId = random_port_no;
回答by satinder singh
Heres the solution.
Blog article : How to add new website in IIS 7
这是解决方案。
博客文章:如何在 IIS 7 中添加新网站
On Button click :
在按钮上单击:
try
{
ServerManager serverMgr = new ServerManager();
string strWebsitename = txtwebsitename.Text; // abc
string strApplicationPool = "DefaultAppPool"; // set your deafultpool :4.0 in IIS
string strhostname = txthostname.Text; //abc.com
string stripaddress = txtipaddress.Text;// ip address
string bindinginfo = stripaddress + ":80:" + strhostname;
//check if website name already exists in IIS
Boolean bWebsite = IsWebsiteExists(strWebsitename);
if (!bWebsite)
{
Site mySite = serverMgr.Sites.Add(strWebsitename.ToString(), "http", bindinginfo, "C:\inetpub\wwwroot\yourWebsite");
mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
mySite.TraceFailedRequestsLogging.Enabled = true;
mySite.TraceFailedRequestsLogging.Directory = "C:\inetpub\customfolder\site";
serverMgr.CommitChanges();
lblmsg.Text = "New website " + strWebsitename + " added sucessfully";
}
else
{
lblmsg.Text = "Name should be unique, " + strWebsitename + " is already exists. ";
}
}
catch (Exception ae)
{
Response.Redirect(ae.Message);
}
Looping over sites whether name already exists
循环站点名称是否已存在
public bool IsWebsiteExists(string strWebsitename)
{
Boolean flagset = false;
SiteCollection sitecollection = serverMgr.Sites;
foreach (Site site in sitecollection)
{
if (site.Name == strWebsitename.ToString())
{
flagset = true;
break;
}
else
{
flagset = false;
}
}
return flagset;
}
回答by Liakat
I have gone though all answer here and also tested. Here is the most clean smarter version of answer for this question. However this still cant work on IIS 6.0. so IIS 8.0 or above is required.
我已经在这里回答了所有问题并进行了测试。这是这个问题最干净、更智能的答案版本。但是,这仍然无法在 IIS 6.0 上工作。所以需要 IIS 8.0 或更高版本。
string domainName = "";
string appPoolName = "";
string webFiles = "C:\Users\John\Desktop\New Folder";
if (IsWebsiteExists(domainName) == false)
{
ServerManager iisManager = new ServerManager();
iisManager.Sites.Add(domainName, "http", "*:8080:", webFiles);
iisManager.ApplicationDefaults.ApplicationPoolName = appPoolName;
iisManager.CommitChanges();
}
else
{
Console.WriteLine("Name Exists already");
}
public static bool IsWebsiteExists(string strWebsitename)
{
ServerManager serverMgr = new ServerManager();
Boolean flagset = false;
SiteCollection sitecollection = serverMgr.Sites;
flagset = sitecollection.Any(x => x.Name == strWebsitename);
return flagset;
}