如何使用C#在IIS上动态添加站点?
我正在ASP.NET中开发一个多商店web应用程序,其中可以创建具有不同域的多个商店。我有一个向导来创建一个存储来填充信息。当我点击向导的“完成”按钮时,我想在IIS上添加特定的存储站点。我如何使用C#实用地做到这一点如何使用C#在IIS上动态添加站点?,c#,asp.net,iis,C#,Asp.net,Iis,我正在ASP.NET中开发一个多商店web应用程序,其中可以创建具有不同域的多个商店。我有一个向导来创建一个存储来填充信息。当我点击向导的“完成”按钮时,我想在IIS上添加特定的存储站点。我如何使用C#实用地做到这一点 我从stackoverflow获得了这段代码,但它引发了异常“指定的HTTPS绑定无效”。我曾经写过这段代码,工作非常完美(IIS7和更高版本): 要在localhost/test/beta1/Customer1/myApplication上托管应用程序,请使用以下参数: sit
我从stackoverflow获得了这段代码,但它引发了异常“指定的HTTPS绑定无效”。我曾经写过这段代码,工作非常完美(IIS7和更高版本): 要在
localhost/test/beta1/Customer1/myApplication
上托管应用程序,请使用以下参数:
siteName
-您在IIS中的站点名称|默认网站
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);
}
}
}
publicstaticvoid CreateVirtualDirectory(此ServerManager iisManager、stringsitename、stringpath、stringphysicalpath)
{
Site Site=iisManager.Sites[siteName];
//删除开头和结尾的“/”
List pathElements=path.Trim(path.altdirectoryseportorchar).Split(path.altdirectoryseportorchar.ToList();
string currentPath=string.Empty;
列表目录路径=路径元素;
//浏览应用程序层次结构,找到最深层的层次结构
Application Application=site.Applications.First(a=>a.Path==Path.altdirectoryseportorchar.ToString());
for(int i=0;i
这里的“CreateVirtualDirectory()”是什么?添加到答案中,很抱歉我忘记了这个。
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;
}
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);
}
}
}