C# ASP.NET 忽略 Web.config 中的 IE7 兼容模式标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1209183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:58:57  来源:igfitidea点击:

ASP.NET Ignores IE7 Compatibility Mode Tag in Web.config

c#asp.netinternet-explorer-8cross-browser

提问by Chris Shouts

I have the following section in my Web.config file

我的 Web.config 文件中有以下部分

<system.webServer>
    <!-- For now..... lets be safe and put IE8 in IE7 compatibility mode-->
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=EmulateIE7" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

but the

但是

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>

is not present in the header of the pages when they are rendered on my site. I followed the advice in this post ASP.NET App - Set IE7-Compatibility Mode?but it does not appear to be working as expected using IE8 and IIS6. Any hints?

当它们在我的网站上呈现时,页面的标题中不存在。我遵循了这篇文章中的建议ASP.NET App - Set IE7-Compatibility Mode? 但使用 IE8 和 IIS6 时它似乎没有按预期工作。任何提示?

采纳答案by Chris Shouts

Turns out the problem was that I'm using IIS6. IIS6 looks at the <system.web>section of Web.config instead of the <system.webServer>section (which is used by IIS7, unless it's running in compatibility mode). To render this meta tag on every page of your site when running IIS6, I believe the best option is to add it to your MasterPage. I ended up adding the following code block to the OnPreRender event of my MasterPage:

原来问题是我使用的是IIS6。IIS6 查看<system.web>Web.config 部分而不是<system.webServer>部分(IIS7 使用的部分,除非它在兼容模式下运行)。要在运行 IIS6 时在站点的每个页面上呈现此元标记,我相信最好的选择是将它添加到您的 MasterPage。我最终将以下代码块添加到 MasterPage 的 OnPreRender 事件中:

Page.Header.Controls.AddAt(0, new HtmlMeta { HttpEquiv = "X-UA-Compatible", Content = "IE=EmulateIE7" });

The reason I used AddAt instead of just plain Add is because the X-UA-Compatible meta tag apparently has to be the first thing in the page header in order for it to be respected.

我使用 AddAt 而不是简单的 Add 的原因是因为 X-UA-Compatible 元标记显然必须是页眉中的第一件事,才能使其受到尊重。

Hope this helps someone in the same boat!

希望这可以帮助同一条船上的人!