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"。