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

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

问题描述

我们已经能够创建一个网站。我们这样做是利用在这个环节的信息:

We have been able to create a web site. We did this using the information in this link:

<一个href=\"http://msdn.microsoft.com/en-us/library/ms525598.aspx\">http://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,有一个新的名为托管API <一个href=\"http://blogs.msdn.com/carlosag/archive/2006/04/17/MicrosoftWebAdministration.aspx\">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).

您将不得不使用互操作服务,并做一些类似的(本例中使用的两​​个对象,服务器和网站,这是存储在服务器的和网站的配置定制类的实例):

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;

查看更多 href=\"http://msdn.microsoft.com/en-us/library/ms524569.aspx\">

See more here

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

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