首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 操作 IIS 7.0 (1) — 建立網站完整實作與應用程式池整合

     編輯:關於C語言

IIS 7.0 程式化管理優勢

IIS 7.0 全模組化、文字設定檔、MMC 圖形化工具,與 .NET 緊密整合。Microsoft.Web.Administration 命名空間提供豐富物件模型,程式碼更少、功能更強。本文整理自專案類庫,涵蓋建立/刪除網站、虛擬目錄、應用程式池、預設文件、Bindings 檢查等。

參考資源:
• ScottGu IIS 7.0 介紹
• MSDN Magazine IIS 7.0 專題

建立網站:通用私有方法 + 公開介面

內含應用程式池建立、身分識別型別、管線模式、Managed Runtime 版本等完整設定:


public static void CreateSite(string siteName, string bindingInfo, string physicalPath) {
    createSite(siteName, "http", bindingInfo, physicalPath, true,
        siteName + "Pool", ProcessModelIdentityType.NetworkService,
        null, null, ManagedPipelineMode.Integrated, null);
}

private static void createSite(string siteName, string protocol, string bindingInformation,
    string physicalPath, bool createAppPool, string appPoolName,
    ProcessModelIdentityType identityType, string appPoolUserName, string appPoolPassWord,
    ManagedPipelineMode appPoolPipelineMode, string managedRuntimeVersion) {

    using (ServerManager mgr = new ServerManager()) {
        Site site = mgr.Sites.Add(siteName, protocol, bindingInformation, physicalPath);

        if (createAppPool) {
            ApplicationPool pool = mgr.ApplicationPools.Add(appPoolName);
            if (pool.ProcessModel.IdentityType != identityType)
                pool.ProcessModel.IdentityType = identityType;
            if (!String.IsNullOrEmpty(appPoolUserName)) {
                pool.ProcessModel.UserName = appPoolUserName;
                pool.ProcessModel.Password = appPoolPassWord;
            }
            if (appPoolPipelineMode != pool.ManagedPipelineMode)
                pool.ManagedPipelineMode = appPoolPipelineMode;

            site.Applications["/"].ApplicationPoolName = pool.Name;
        }
        mgr.CommitChanges();
    }
}

bindingInfo 格式:"*:<port>:<hostname>",例 "*:80:myhost.com"。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved