当前位置:Gxlcms > asp.net > 使用ASP.NET操作IIS7中应用程序

使用ASP.NET操作IIS7中应用程序

时间:2021-07-01 10:21:17 帮助过:29人阅读

在最新发布的启明星Portal里,增加了安装程序,下面说一下.NET对IIS7操作。IIS7的操作和IIS5/6有很大的不同,在IIS7里增加了 Microsoft.Web.Administration 命名空间里,增加了ServerManager、Site几个大类来操作IIS7。

下面是一些核心代码,可以直接使用

建立虚拟目录

建立虚拟目录时,默认使用“Default Web Site”,也就是默认建立在Default Web Site, CreateVdir需要两个参数:虚拟路径名称和实际的物理路径.

  1. public static bool CreateVdir(string vdir, string phydir)
  2. {
  3. ServerManager serverManager = new ServerManager();
  4. Site mySite = serverManager.Sites["Default Web Site"];
  5. mySite.Applications.Add("/" + vdir, phydir); serverManager.CommitChanges();
  6. return true;
  7. }

这里建立的是在Default Web Site下的虚拟目录,将上面的mysite修改为

  1. Site mySite = iisManager.Sites.Add("test", "http", "*:80:" + WebName + ".intranet." + TLD, @"c:\Webs\" + WebName);

则可以建立网站。这2个区别是:你建立一个网站。前面的访问示意URL是 http://www.dotnetcms.org/book ,而后者是http://book.dotnetcms.org

接下来创建应用程序池

  1. public static void CreateAppPool( string appPoolName)
  2. {
  3. try {
  4. ServerManager serverManager = new ServerManager();
  5. serverManager.ApplicationPools.Add(appPoolName);
  6. ApplicationPool apppool = serverManager.ApplicationPools[appPoolName];
  7. apppool.ManagedPipelineMode = ManagedPipelineMode.Classic;
  8. serverManager.CommitChanges();
  9. apppool.Recycle(); }
  10. catch { }
  11. }

这里ManagedPipelineMode的取值 ManagedPipelineMode.Classic;IIS7支持经典Classic方式和Interget集成方式,在集成方式下

自定义的handler和Module可能无效,如果你想和以前IIS5/6版本兼容可以使用Classic方式,否则建议使用集成方式。

下面代码演示了如何把虚拟目录分配到应用程序池,和IIS5/6最大的区别是vdir其实是vdir path,所以这里加了一个“/”,表示一个虚路径。

  1. public static void AssignVDirToAppPool(string vdir, string appPoolName)
  2. {
  3. try
  4. {
  5. ServerManager serverManager = new ServerManager();
  6. Site site = serverManager.Sites["Default Web Site"];
  7. site.Applications["/" + vdir].ApplicationPoolName = appPoolName;
  8. serverManager.CommitChanges();
  9. }
  10. catch { }
  11. }

最后增加一个删除操作

  1. public static bool DeleteVdir(string vDirName)
  2. {
  3. try
  4. {
  5. ServerManager serverManager = new ServerManager();
  6. Site mySite = serverManager.Sites["Default Web Site"];
  7. Microsoft.Web.Administration.Application application = mySite.Applications["/" + vDirName];
  8. mySite.Applications.Remove(application);
  9. serverManager.CommitChanges();
  10. return true;
  11. }
  12. catch {
  13. return false;
  14. }
  15. }

到此,.NET操作IIS7的基本功能已经实现了,希望对大家的学习有所帮助。

人气教程排行