c# - 使用 Microsoft.Web.Administration 库创建 IIS 应用程序会创建两个应用程序而不是一个

标签 c# .net iis iis-7

我正在尝试自动创建 IIS 应用程序,为此我正在使用 Microsoft.Web.Administration图书馆。这是我正在使用的部分代码:

IISHelper.cs

public static void CreateApplicationPool(string applicationPoolName)
{
    using (ServerManager serverManager = new ServerManager())
    {
        if (serverManager.ApplicationPools[applicationPoolName] != null)
            return;
        ApplicationPool newPool = serverManager.ApplicationPools.Add(applicationPoolName);
        newPool.ManagedRuntimeVersion = "v4.0";
        serverManager.CommitChanges();
    }
}

public static void CreateSite(string siteName, string path)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var sites = serverManager.Sites;
        if (sites[siteName] == null)
        {
            sites.Add(siteName, "http", "*:80:", path);
            serverManager.CommitChanges();
        }
    }
}

public static void CreateApplication(string siteName, string applicationName, string path)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var site = GetSite(serverManager, siteName);
        var applications = site.Applications;
        if (applications["/" + applicationName] == null)
        {
            applications.Add("/" + applicationName, path);
            serverManager.CommitChanges();
        }
    }
}

public static void CreateVirtualDirectory(string siteName, string applicationName, string virtualDirectoryName, string path)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var application = GetApplication(serverManager, siteName, applicationName);
        application.VirtualDirectories.Add("/" + virtualDirectoryName, path);
        serverManager.CommitChanges();
    }
}

public static void SetApplicationApplicationPool(string siteName, string applicationPoolName)
{
    using (ServerManager serverManager = new ServerManager())
    {
        var site = GetSite(serverManager, siteName);
        if (site != null)
        {
            foreach (Application app in site.Applications)
            {
                app.ApplicationPoolName = applicationPoolName;
            }
        }                
        serverManager.CommitChanges();
    }
}

这里是调用上述 IISHelper 类方法的主要代码

string siteName = "TestSite";
string applicationName = "TestApp";
string virtualDirectoryName = "TestVirtualDirectory";
string applicationPoolName = "TestAppPool";
string physicalPath = @"C:\inetpub\mynewsite";

IISHelper.CreateApplicationPool(applicationPoolName);
IISHelper.CreateSite(siteName, physicalPath);
IISHelper.CreateApplication(siteName, applicationName, physicalPath);
IISHelper.CreateVirtualDirectory(siteName, applicationName, virtualDirectoryName, physicalPath);
IISHelper.SetApplicationApplicationPool(siteName, applicationPoolName); 

执行此代码后,我在 IIS 下成功创建了具有站点和适当应用程序池的应用程序,但是当我在新创建的应用程序池的菜单中选择“查看应用程序”时,我看到有两个应用程序,但我确定必须只有一个应用程序。我正在努力避免创建两个应用程序但找不到方法。这是一张说明这一点的图片:

enter image description here

所以这是我的问题:为什么在执行上述代码后会创建两个应用程序以及如何修改代码以避免该问题?

最佳答案

每个站点都必须包含一个“根应用程序”,也就是您所看到的第二个应用程序。基本上,当您在上面请求您的网站时 ' http://example.com/ ' (或您在绑定(bind)中设置的任何主机名)您将从该根应用程序请求,此外您正在创建/TestApp ,这将在调用 ' http://example.com/TestApp 时被请求'.当您创建站点并提供物理路径时,它会在内部为其创建一个根应用程序“/”。 要查看此内容,您可以打开 %windir%\system32\inetsrv\applicationHost.config。

所以从技术上讲,您所看到的是正确的,但您可能实际上并不想要一个新应用程序?只需要根应用程序?您真的需要/TestApp 出现在 URL 中吗?

关于c# - 使用 Microsoft.Web.Administration 库创建 IIS 应用程序会创建两个应用程序而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417578/

相关文章:

.net - 用于 .NET 的轻量级 x86 模拟器/在托管环境中执行 x86 代码

.net - 有人知道 ASP.NET MVC 4 的进展吗

iis - 无法获取 IIS URL 重写规则以修复 URL 中的 %3F 和 %3D

php - odbc_连接sql服务器

c# - WebRequest 到 POST 数据但是隐藏字段呢?

c# - 错误 : The name 'ConfigurationManager' does not exist in the current context

c# - 无法从 DeviceInformation.Id 创建 UsbDevice

c# - Nuget 防止在解决方案中生成包文件夹

.net - 如何将 C++ 字符串转换为 .NET 字符串^?

asp.net - 如何记录在 IIS 6.0 上运行的 WebService 的通信?