.net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联

时间:2023-01-20 21:01:47

ServerManager类用来操作IIS,提供了很多操作IIS的API。使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%windir%\System32\Inetsrv\Microsoft.Web.Administration.dll

以下代码展示了如何将自己的ASP.NET网站自动部署到IIS的过程,其中包括,新建网站-》新建应用程序池-》设置网站和应用程序池的关联-》重启网站和应用程序池

具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration; namespace AutoConfigureIIS
{
internal class Program
{
//don't forget to add a reference to %windir%\System32\Inetsrv\Microsoft.Web.Administration.dll private static ServerManager serverMgr = new ServerManager();
private static string applicationPoolName = "IPSRemoteApplicationPool";
private static string applicationPoolVersion = "v4.0";
private static string websiteName = "IPSRemoteSite";
private static string port = "";
private static string webPath = @"C:\Work\Published\SelfHostedIPSRemoteWeb"; private static void Main(string[] args)
{
DeployIPSWeb(); Console.Read();
} /// <summary>
/// Auto Configure IPS WebSite to IIS
/// </summary>
private static void DeployIPSWeb()
{
//create Application Pool
if (!IsApplicationPoolExists(applicationPoolName))
{
CreateApplicationPool(applicationPoolName);
}
//create website
if (!IsWebsiteExists(websiteName))
{
CreateWebSite(websiteName, port, webPath);
} Site site = FindSiteBySiteName(websiteName); // get site by Index or by siteName
ApplicationPool appPool = FindApplicationPoolByName(applicationPoolName);
// get appPool by Index or by appPoolName site.Stop();
site.ApplicationDefaults.ApplicationPoolName = appPool.Name; foreach (var item in site.Applications)
{
item.ApplicationPoolName = appPool.Name;
} serverMgr.CommitChanges(); // this one is crucial!!! see MSDN:
// Updates made to configuration objects must be explicitly written to the configuration
// system by using the CommitChanges method!!
site.Start();
appPool.Recycle();
} /// <summary>
/// Create WebSite sitename to IIS with the arguments
/// </summary>
/// <param name="sitename"></param>
/// <param name="portstr"></param>
/// <param name="webPathstr">website code path</param>
public static void CreateWebSite(string sitename, string portstr, string webPathstr)
{
serverMgr.Sites.Add(sitename, "http", "*:" + portstr + ":", webPathstr); serverMgr.CommitChanges();
} public static void CreateApplicationPool(string poolname)
{
ApplicationPool newPool = serverMgr.ApplicationPools.Add(poolname);
newPool.ManagedRuntimeVersion = applicationPoolVersion;
serverMgr.CommitChanges();
} public static bool IsWebsiteExists(string strWebsitename)
{
Site site = FindSiteBySiteName(strWebsitename);
if (site == null)
return false;
else
return true;
} public static Site FindSiteBySiteName(string strWebsitename)
{
SiteCollection sitecollection = serverMgr.Sites;
foreach (Site site in sitecollection)
{
if (site.Name == strWebsitename.ToString())
{
return site;
}
}
return null;
} public static ApplicationPool FindApplicationPoolByName(string poolName)
{
ApplicationPoolCollection poolcollection = serverMgr.ApplicationPools;
foreach (ApplicationPool pool in poolcollection)
{
if (pool.Name == poolName.ToString())
{
return pool;
}
}
return null;
} public static bool IsApplicationPoolExists(string appPool)
{
ApplicationPool pool = FindApplicationPoolByName(appPool);
if (pool == null)
{
return false;
}
else
{
return true;
}
}
}
}