[英]How to add site on IIS dynamically using C#?
我正在ASP.NET中開發一個多商店Web應用程序,可以在其中創建具有不同域的多個商店。 我有一個向導來創建商店來填充信息。 當我單擊向導的完成按鈕時,我想要什么,我想在IIS上添加特定的商店站點。 我如何使用C#實用地做到這一點。
private void ConfigureSiteInIis()
{
string strWebsitename = txtStoreName.Text; // abc
const string strApplicationPool = "DefaultAppPool"; // set your deafultpool :4.0 in IIS
string strhostname = txtDomainName.Text; //abc.com
const string stripaddress = "localhost:40411"; // ip address
string bindinginfo = stripaddress + ":80:" + strhostname;
var serverMgr=new ServerManager();
//Site mySite = serverMgr.Sites.Add(txtStoreName.Text, "http", bindinginfo, "C:\\inetpub\\wwwroot\\yourWebsite");
Site mySite = serverMgr.Sites.Add(txtStoreName.Text, "http", "*:80:" + strhostname , Server.InetPath+txtDomainName.Text);
mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
mySite.TraceFailedRequestsLogging.Enabled = true;
mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
serverMgr.CommitChanges();
lblMessage.Text = "New website " + strWebsitename + " added sucessfully";
lblMessage.ForeColor = System.Drawing.Color.Green;
}
我從stackoverflow那里獲得了這段代碼,但是它拋出異常“指定的HTTPS綁定無效”。
我曾經寫過這篇文章,可以很好地工作(IIS7和更高版本):
public static Application CreateApplicaiton(string siteName, string path, string physicalPath, string appPoolName)
{
ServerManager iisManager = ServerManager.OpenRemote(Environment.MachineName.ToLower());
// should start with "/" also not to end with this symbol
string correctApplicationPath = string.Format("{0}{1}", Path.AltDirectorySeparatorChar, path.Trim(Path.AltDirectorySeparatorChar));
int indexOfApplication = correctApplicationPath.LastIndexOf(Path.AltDirectorySeparatorChar);
if (indexOfApplication > 0)
{
// create sequence of virtual directories if the path is not a root level (i.e. test/beta1/Customer1/myApplication)
string virtualDirectoryPath = correctApplicationPath.Substring(0, indexOfApplication);
iisManager.CreateVirtualDirectory(siteName, virtualDirectoryPath, string.Empty);
}
Application application = iisManager.Sites[siteName].Applications.Add(correctApplicationPath, physicalPath);
application.ApplicationPoolName = appPoolName;
return application;
}
要在localhost/test/beta1/Customer1/myApplication
上托管應用程序,請使用以下參數:
siteName
您在IIS中的站點名稱| Default Web Site
path
-虛擬目錄| test/beta1/Customer1/myApplication
public static void CreateVirtualDirectory(this ServerManager iisManager, string siteName, string path, string physicalPath)
{
Site site = iisManager.Sites[siteName];
//remove '/' at the beginning and at the end
List<string> pathElements = path.Trim(Path.AltDirectorySeparatorChar).Split(Path.AltDirectorySeparatorChar).ToList();
string currentPath = string.Empty;
List<string> directoryPath = pathElements;
//go through applications hierarchy and find the deepest one
Application application = site.Applications.First(a => a.Path == Path.AltDirectorySeparatorChar.ToString());
for (int i = 0; i < pathElements.Count; i++)
{
string pathElement = pathElements[i];
currentPath = string.Join(Path.AltDirectorySeparatorChar.ToString(), currentPath, pathElement);
if (site.Applications[currentPath] != null)
{
application = site.Applications[currentPath];
if (i != pathElements.Count - 1)
{
directoryPath = pathElements.GetRange(i + 1, pathElements.Count - i - 1);
}
}
}
currentPath = string.Empty;
foreach (string pathElement in directoryPath)
{
currentPath = string.Join(Path.AltDirectorySeparatorChar.ToString(), currentPath, pathElement);
//add virtual directories
if (application.VirtualDirectories[currentPath] == null)
{
//assign physical path of application root folder by default
string currentPhysicalPath = Path.Combine(application.VirtualDirectories[0].PhysicalPath, pathElement);
//if this is last element of path, use physicalPath specified on method call
if (pathElement == pathElements.Last() && !string.IsNullOrWhiteSpace(physicalPath))
{
currentPhysicalPath = physicalPath;
}
currentPhysicalPath = Environment.ExpandEnvironmentVariables(currentPhysicalPath);
if (!Directory.Exists(currentPhysicalPath))
{
Directory.CreateDirectory(currentPhysicalPath);
}
application.VirtualDirectories.Add(currentPath, currentPhysicalPath);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.