简体   繁体   中英

At what point do I need to add the X-UA Compatible meta tags?

I'm trying to add X-UA Compatible meta tags to my .net project by overriding a page method but I'm not exactly sure at what stage I need to add the tags. At the moment I have:

    protected override void OnInit(EventArgs e)
    {
        HtmlMeta meta = new HtmlMeta();
        meta.HttpEquiv = "X-UA-Compatible";
        meta.Content = "IE=EmulateIE7";
        Page.Header.Controls.Add(meta);
        base.OnInit(e);
    }

but this doesn't appear to be working. Do I have to add the tags before the page has initialized? On load? Any help would be appreciated.

I would do this via the web.config:

<configuration>
   <system.webServer>
      <httpProtocol>
         <customHeaders>
            <add name="X-UA-Compatible" value="IE=EmulateIE7" />
         </customHeaders>
      </httpProtocol>
   </system.webServer>
</configuration>

Will work and requires no changes to your code.

More information: http://www.iis.net/configreference/system.webserver/httpprotocol/customheaders

Generally speaking, you should not add it. Please check the relevant MSDN article about specifying document models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM