[英]How to add site on IIS dynamically using C#?
I am developing a multi store web application in ASP.NET where multiple stores with different domain can be created. 我正在ASP.NET中开发一个多商店Web应用程序,可以在其中创建具有不同域的多个商店。 I have a wizard to create a store to fill the information.
我有一个向导来创建商店来填充信息。 what i want when i clik on finish button of wizard, i want to add the particular store site on IIS.
当我单击向导的完成按钮时,我想要什么,我想在IIS上添加特定的商店站点。 how can i do that pragmatically using C#.
我如何使用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;
}
I have got this code from stackoverflow but it is throwing exception "The specified HTTPS binding is invalid". 我从stackoverflow那里获得了这段代码,但是它抛出异常“指定的HTTPS绑定无效”。
I've wrote this one time ago, works perfectly(IIS7 and later): 我曾经写过这篇文章,可以很好地工作(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;
}
to host an application at localhost/test/beta1/Customer1/myApplication
, use following parameters: 要在
localhost/test/beta1/Customer1/myApplication
上托管应用程序,请使用以下参数:
siteName
- name of your site in IIS | siteName
您在IIS中的站点名称| Default Web Site
path
- virtual directory | 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.