IIS 7.5 applicationHost.config文件未更新

问题描述:

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

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

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

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

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

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.

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

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.

有人可以解释一下吗?我是否缺少某些内容(也许看错了文件?)?有人可以说明SysWOW64\inetsrv\config目录吗?

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?

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

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

我刚刚尝试了您的代码,但效果很好.您知道这种mime类型是添加到全局mime类型集合中,而不是网站中吗?

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?

它也被添加到<staticContent>列表的末尾,当您执行ServerManager.CommitChanges()时,此列表不会重新排序.

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

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

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

C:\Windows\System32\inetsrv\config

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

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.

还尝试添加诸如.xxx之类的异常内容,运行更新,然后打开配置文件并进行搜索.我保证会在那里.

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.

更新:

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

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.

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

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.