IIS 7.5 applicationHost.config 文件未更新

发布于 2024-11-02 03:05:37 字数 1793 浏览 2 评论 0原文

我目前正在使用 Microsoft.Web.Administration (MWA) 命名空间,以便调整我们的应用程序以使用新 API 配置 IIS 7.5。 我知道所有 IIS 级别更改都应在以下文件中表示(我使用的是 Win2K8-R2):

%WINDIR%\System32\inetsrv\config\applicationHost.config

因此,当我使用 ServerManager 对象提交配置更改时,文件应相应更新。

添加新的 MIME 类型(使用 MWA 编程)后,我没有在 applicationHost.config 文件 中看到任何更改,但我确实在 IIS 管理器窗口中看到了新的 MIME 类型,并且 IIS 识别了此 MIME 类型没有问题。即使在重新启动操作系统之后 - 配置文件也不包含新添加的 MIME 类型,但 IIS 管理器窗口确实列出了它。

由于我的应用程序池被强制为 32 位(Enable32BitAppOnWin64 = true),我认为相关的配置文件应位于 %WINDIR%\SysWOW64\inetsrv\Config 下,但是(如果存在...) - 在代码提交更新后它也不会改变。

有人可以解释一下吗?我是否遗漏了一些东西(也许看错了文件?)?有人可以解释一下 SysWOW64\inetsrv\config 目录吗?

这是我添加 MIME 类型的代码:

ServerManager manager = new ServerManager();
ConfigurationElementCollection staticContentCollection = manager
    .GetApplicationHostConfiguration()
    .GetSection("system.webServer/staticContent")
    .GetCollection();

//MIMETypes is a string[] array, each object is {FileExt},{MIMETypeStr}
foreach (string pair in MIMETypes)
{
    string[] mimeProps = pair.Split(',');

    ConfigurationElement mimeTypeEl = staticContentCollection
          .Where(a => 
                   (string)a.Attributes["fileExtension"].Value == mimeProps[0])
          .FirstOrDefault();


    if (mimeTypeEl != null)
    {
        staticContentCollection.Remove(mimeTypeEl);
    }

    ConfigurationElement mimeMapElement = 
                  staticContentCollection.CreateElement("mimeMap");

    mimeMapElement["fileExtension"] = mimeProps[0];
    mimeMapElement["mimeType"] = mimeProps[1];

    staticContentCollection.Add(mimeMapElement);
}

manager.CommitChanges();

//At this point all is working but the config file does not reflect the change

I'm currently playing around with the Microsoft.Web.Administration (MWA) namespace in order to adjust our application to configure IIS 7.5 with the new API.
I understood that all IIS level changes should be expressed in the following file (I'm on Win2K8-R2):

%WINDIR%\System32\inetsrv\config\applicationHost.config

So, when I use the ServerManager object to commit the configuration changes the file should be updated accordingly.

After adding a new MIME type (programmatic with MWA) I did not see any changes in the applicationHost.config file, but I do see the new MIME type in the IIS manager window and IIS recognizes this MIME type without problems. Even after restating the OS - The config file does not contain the newly added MIME type, but the IIS manager window does list it.

Because my application pools are forced to 32-bit (Enable32BitAppOnWin64 = true), I thought that the related config file should be located under %WINDIR%\SysWOW64\inetsrv\Config, but (if it exists...) - it also does not change after the code commits the updates.

Can someone please explain this? Am I missing something (looking at the wrong file maybe?)? Can someone please shed some light on the SysWOW64\inetsrv\config directory?

This is my code for adding the MIME type:

ServerManager manager = new ServerManager();
ConfigurationElementCollection staticContentCollection = manager
    .GetApplicationHostConfiguration()
    .GetSection("system.webServer/staticContent")
    .GetCollection();

//MIMETypes is a string[] array, each object is {FileExt},{MIMETypeStr}
foreach (string pair in MIMETypes)
{
    string[] mimeProps = pair.Split(',');

    ConfigurationElement mimeTypeEl = staticContentCollection
          .Where(a => 
                   (string)a.Attributes["fileExtension"].Value == mimeProps[0])
          .FirstOrDefault();


    if (mimeTypeEl != null)
    {
        staticContentCollection.Remove(mimeTypeEl);
    }

    ConfigurationElement mimeMapElement = 
                  staticContentCollection.CreateElement("mimeMap");

    mimeMapElement["fileExtension"] = mimeProps[0];
    mimeMapElement["mimeType"] = mimeProps[1];

    staticContentCollection.Add(mimeMapElement);
}

manager.CommitChanges();

//At this point all is working but the config file does not reflect the change

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糯酥胸 2024-11-09 03:05:37

我刚刚尝试了你的代码,它工作正常。您知道此 MIME 类型正在添加到全局 MIME 类型集合中,而不是添加到站点中吗?

它还会添加到 列表的末尾,当您执行 ServerManager.CommitChanges() 时,该列表不会重新排序。

另外,在 Windows 2008-R2 上,applicationHost.config 的正确位置位于:

C:\Windows\System32\inetsrv\config

我猜您正在使用 notepad.exe 或 NotePad2 打开此文件(32 位编辑器无法打开它) )。记事本不会在更改时重新加载文件,并且需要告诉 NotePad2 显示文件更改通知 (alt-F5),开箱即用它不会。

还可以尝试添加一些不寻常的内容,例如 .xxx,运行更新,然后打开配置文件并进行搜索。我保证它会在那里。

更新:

根据您在下面的评论,我不确定您如何使用 NotePad++ 或任何 32 位编辑器打开 applicationHost.config,我当然可以' t。您可以下载64位编辑器NotePad2吗:

http://www.flos-freeware.ch/notepad2.html

候选版本运行得很好。

在任何 64 位 Windows 2008 或 Windows 7 的默认安装中,C:\Windows\SysWOW64\inetsrv\Config 文件夹中不应有 applicationHost.config。我不确定你为什么会在那里看到一个。

I just tried your code and it works fine. You are aware that this mime type is being added to the global mime type collection and not to a site?

It also gets added to the end of the <staticContent> list, this list isn't re-sorted when you do ServerManager.CommitChanges().

Also on Windows 2008-R2 the correct location for applicationHost.config is at:

C:\Windows\System32\inetsrv\config

I'm guess you're either using notepad.exe or NotePad2 to open this file (32 bit editors can't open it). Notepad won't reload the file upon a change and NotePad2 needs to be told to display a file change notification (alt-F5), out of the box it won't.

Also try adding something unusual like .xxx, run your update then open the config file and do a search. I guarantee it'll be there.

Update:

Further to your comments below, I'm not sure how you're able to open applicationHost.config using NotePad++ or any 32-bit editor, I certainly can't. Can you download NotePad2 which is a 64-bit editor:

http://www.flos-freeware.ch/notepad2.html

The release candidate works just fine.

On a default install of any 64 bit Windows 2008 or Windows 7 there shouldn't be an applicationHost.config in the C:\Windows\SysWOW64\inetsrv\Config folder. I'm not sure why you'd be seeing one there.

白芷 2024-11-09 03:05:37

作为使用您最喜欢的 64 位兼容的 32 位编辑器(即 Notepad++)打开和编辑 64 位 IIS 配置文件的解决方法,您可以创建一个指向 C:\Windows 的 Windows 目录符号链接\System32\inetsrv\Config。使用此方法,您将替换位于C:\Windows\SysWOW64\inetsrv\Config的32位Config目录以指向64 位版本。例如,如果您的应用程序同时需要 32 位和 64 位版本,则此方法将不起作用。

有关更多信息,我强烈建议您访问 此 MSDN 博客

As a workaround to open and edit the 64-bit IIS configuration files with your favorite 32-bit editor that is 64-bit compatible (i.e. Notepad++), you can create a Windows directory symbolic link which points to C:\Windows\System32\inetsrv\Config. With this method, you are replacing the 32-bit Config directory, located at C:\Windows\SysWOW64\inetsrv\Config to point to the 64-bit version. If, for example, you have an application which requires both 32-bit and 64-bit versions, this method won't work.

For more information, I strongly encourage you to visit this MSDN Blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文