IIS displaying page differently when localhost is used in URL vs. hostname

I'm having (yet another) strange problem with IIS. When viewing an ASPX page I've designed on my local machine by browsing to http://localhost/page.aspx the page looks as expected (and looks the same in IE, Firefox and Chrome. If I change localhost to my_hostname the page is rendered with a disabled vertical scroll bar.

The behavior was first noticed when I published my site to our live server and saw the same discrepancy. After beating my head against the wall I tried what I described above and was able to duplicate my "problem". So with that, I turn to you guys.

This wouldn't really be an issue (save for the cross-browser inconsistency) except that this screws up an "absolute"ly positioned <div> moving it partway off the screen instead of being centered like it should be (and is when viewed any other way except in IE when the address is anything but localhost).


As another test I added a new aspx page to my project and didn't add or change any of the default code. If I browse to the page using localhost there is no scrollbar. If I browse to the page using my_hostname the scrollbar is there. Whatever the difference is it's making IE's processing of CSS get screwed up, to the point where at first everything works the same in all browsers I'm testing in, and afterwards IE just makes up its own rules. This is incredibly frustrating and I'm really hoping I'm just doing something wrong and it's not an inherent problem.


Solution 1:

I know this is an old thread, but I just hit the same problem. If you are using IE8, the issue may be its Compatibility View. By default, sites in your local intranet - but NOT localhost - are rendered in IE7 compatibility view. More info here:

http://msdn.microsoft.com/en-us/library/cc288325%28VS.85%29.aspx

Unfortunately, that doesn't help get rid of the disabled scrollbar, but it does explain the discrepancy.

Solution 2:

The problem resides in the IE8 compatability view settings. By default, intranet sites (your server) are displayed in compatibility view. To override this behaviour you should add the following code to your code behind of your aspx page.

protected override void OnPreInit(EventArgs e) {
    Response.AddHeader("X-UA-Compatible", "IE=8");       

    base.OnPreInit(e);
}

It worked for me.

Solution 3:

In IE9, sites running on localhost are automatically rendered in Compability Mode. To change this (default) behaviour, do this:

  1. If not activiated, activate the toolbar Command
  2. Click Page > Settings for Compabilitymode
  3. Uncheck "Display intranetsites in compabilitymode"

Solution 4:

Here's a post on StackOverflow about this

basically change the top of your HTML Layout or MasterPage(after the <%@...%>) to:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" />