使用C#以编程方式在IIS中创建网站并设置端口号 [英] Programmatically create a web site in IIS using C# and set port number

查看:108
本文介绍了使用C#以编程方式在IIS中创建网站并设置端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经能够创建一个网站。我们使用此链接中的信息完成了此操作:

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

但是,我们希望使用端口号80以外的端口号。我们怎么做?

However, we would like to use a port number other that port 80. How do we do this?

我们正在使用IIS 6

We are using IIS 6

推荐答案

如果您使用的是IIS 7,则会有一个名为 Microsoft.Web.Administration

If you're using IIS 7, there is a new managed API called Microsoft.Web.Administration

以上博文中的一个例子:

An example from the above blog post:

ServerManager iisManager = new ServerManager();
iisManager.Sites.Add("NewSite", "http", "*:8080:", "d:\\MySite");
iisManager.CommitChanges(); 

如果您正在使用IIS 6并希望这样做,那么遗憾的是它更复杂。

If you're using IIS 6 and want to do this, it's more complex unfortunately.

您必须在每台服务器上创建一个Web服务,这是一个处理网站创建的Web服务,因为通过网络的直接用户模拟将无法正常工作(如果我没记错了。)

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).

你将不得不使用Interop Services并做类似的事情(这个例子使用两个对象,服务器和站点,它们是实例存储服务器和站点配置的自定义类:

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):

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;

查看更多这里

这篇关于使用C#以编程方式在IIS中创建网站并设置端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆