144

My all applications were working fine but suddenly all sites under IIS are not loading css, images, scripts. It redirect to login page.

If i login it works fine. e.g. mysite.com/Account/LogOn?ReturnUrl=%2fpublic%2fimages%2ficons%2f41.png

On my local machine it works fine without login.

tereško
  • 58,060
  • 25
  • 98
  • 150
Imran Rashid
  • 3,352
  • 4
  • 31
  • 41
  • wrong tagging, not real question... – Renatas M. May 09 '12 at 08:03
  • 3
    Rollback to tagged version because I *think* I know what is causing this behavior and the tags are relevant. If not, I will vote to close. – Tim M. May 09 '12 at 08:07
  • It is not problem with this web config i have tried all the answers. I think it is problem with IIS because all sites was working fine before i deploy one application it effect all on iis – Imran Rashid May 09 '12 at 09:01
  • @ImranRashid - is this site inheriting settings from another site in IIS? – Tim M. May 09 '12 at 09:17
  • @Tim Medora - No sir it is not inheriting from other site. – Imran Rashid May 09 '12 at 09:26
  • I resolve it myself. The problem arise when i share wwwroot folder on my network. It change the permission of directory. When i unshare and set permission to default. It resolved – Imran Rashid May 10 '12 at 05:33

26 Answers26

214

The problem may be that IIS is not serving Static Content, which you can set up here: enter image description here

Source: http://adilmughal.com/blog/2011/11/iis-7-not-loading-css-and-image/

Windows 10:

windows 10 version of the above dialog

Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79
213

I had the same problem, an unauthenticated page would not load the CSS, JS and Images when I installed my web application in ASP.Net 4.5 in IIS 8.5 on Windows Server 2012 R2.

  1. I had the static content role installed
  2. My Web Application was in the wwwroot folder of IIS and all the Windows Folder permissions were intact (the default ones, including IIS_IUSRS)
  3. I added authorization for all the folders that contained the CSS, JS and images.
  4. I had the web application folder on a windows share, so I removed the sharing as suggested by @imran-rashid

Yet, nothing seemed to solved the problem. Then finally I tried setting the identity of the anonymous user to the App Pool Identity and it started working.

Click on the Authentication Feature Edit the Anonymous Authentication Change to App Pool Identity

I banged my head for a few hours and hope that this response will save the agony for my fellow developers.

I would really like to know why this is working. Any thoughts?

adam0101
  • 29,096
  • 21
  • 96
  • 174
Moiz Tankiwala
  • 6,070
  • 7
  • 38
  • 51
  • 2
    This also worked for me. I believe this is because the "Specific user" did not have permissions to the folder location, where the identity of the application pool does. – dcinadr May 05 '15 at 23:38
  • 5
    Few hours ? It tooks me more than 4. Thanks god it helped me :) – Tito May 21 '15 at 14:08
  • 3
    I got this problem suddenly, after sharing the wwwroot folder. Your solution fixed it. Thanks for taking the time to add it, and for the detail. – Resource Jun 04 '15 at 08:26
  • Wow. No telling how long I would have been trying to track this issue down. Thanks! – jwatts1980 Dec 13 '15 at 18:59
  • Great! When I've fot these errors insetad of styles and scripts - I thought that I'm totally messed up - but this solution really saved me. Similar to user above it seems to be caused by folder sharing, which is needed for deployment. – Usurer Feb 16 '16 at 10:14
  • @Moiz Tankiwala Thanks for your support...Thank you very much – Poorna Mar 18 '16 at 19:40
  • 1
    LEG-END! Thanks so much – David Jul 06 '16 at 09:38
  • There would have been no way I could have figured this out on my own - this was my solution too. In my case I wanted to add my Visual Studio Community project (in my documents folder) to be accessible by another computer browser by adding it as an application to my local IIS (inetpub/wwwroot) without pasting all the files. Doing this worked for me, now all content is served. – Vasily Hall Aug 09 '16 at 19:14
  • 1
    This is the 2nd time I had to search for your post, b/c it saved me twice now. I'd upvote you 100x if I could. Was using Umbraco - set up my local IIS using Documents\Visual Studio 2013\Projects - added `CPUName\IIS_IUSRS` and still wouldn't load. Thanks again! – Rob Scott Aug 30 '16 at 23:19
  • 1
    That was it! Thanks! – Peter Pompeii Feb 09 '17 at 18:29
  • 1
    You beauty!! Thanks. – PurpleSmurph Aug 22 '18 at 19:28
  • 1
    This answer deserves all the +'s that can be thrown at it! – AbdulG Nov 01 '20 at 10:19
  • 7 years old answer and still going strong! thank you, you helped me solve my issue!! – Avrgebro Jun 03 '22 at 19:54
  • Had the same problem with a Windows 2019 server and this solved my issue. – nbstrat Sep 06 '22 at 18:24
  • Brilliant! I spent so much time trying to solve it. Thank you! – Altav1sta Jan 26 '23 at 04:08
21

I had a similar error, my console looked like this:

error

My problem was that I was running my site in a sub folder since the company was using one top domain and no sub domains. Like this:

host.com/app1

host.com/app2

My code looked like this for including scripts which worked fine on localhost but not in app1 or app2:

<link rel="stylesheet" type="text/css" href="/Content/css/font-awesome.min.css" />

Added a tilde sign ~ to src and then everything worked:

<link rel="stylesheet" type="text/css" href="~/Content/css/font-awesome.min.css" />

Explanation of ~ vs /:

  • / - Site root
  • ~/ - Root directory of the application

/ will return the root of the site (http://host.com/),

~/ will return the root of the application (http://host.com/app1/).

Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • 2
    up vote. I was having this problem, the app was nothing loading on stage but loading on production. I use @Url.Content() too with ~/. Not tested on production, but I think it'll works – Kross Feb 28 '19 at 21:25
18

Try removing the staticContent section from your web.config.

<system.webServer>
    <staticContent>
        ...
    </staticContent>
</system.webServer>
intrepidis
  • 2,870
  • 1
  • 34
  • 36
15

This might not answer your question but I've been banging my head with the same symptoms with a new IIS installation. CSS, JS and images were not showing up. Was due to the "Static Content" role not being installed in IIS 7.5.

user23462
  • 221
  • 2
  • 4
  • You might have different issue and my issue was when i move directory from inetpub it remove permissions when i revert my issue was fixed. – Imran Rashid Jul 31 '13 at 15:44
8

You probably have Windows authentication enabled in your web.config. On a local machine, your Windows credentials are automatically passed and it works. On a live site, you are treated as an anonymous user (IE setting can control this, but don't modify this unless you really know what you are doing).

This causes the following:

  • You are required to explicitly login.
  • Resources like scripts and CSS are not served on the login page because you are not authenticated.

This isn't broken, just working as intended, but to "fix" this:

  • Change the authentication type in the web.config if you don't want any login.
  • And/or add a web.config in the directory(s) containing CSS, images, scripts, etc. which specifies authorization rules.
Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • authentication mode is Forms not window? application web config file override machine config ? Is there any setting in IIS which is causing this? – Imran Rashid May 09 '12 at 09:14
  • Forms authentication will also trigger a login prompt and prevent resources like scripts and images from being served. See the other answers for the specific changes you would need to make to web.config. Saving settings in IIS will modify the web config. – Tim M. May 09 '12 at 09:15
  • I resolve it myself. The problem arise when i share wwwroot folder on my network. It change the permission of directory. When i unshare and set permission to default. It resolved :) Thanks Tim – Imran Rashid May 10 '12 at 05:33
7

It was windows permission issue i move the folder thats it inherit wrong permissions. When i move to wwwroot folder and add permission to iis user it start working fine.

Imran Rashid
  • 3,352
  • 4
  • 31
  • 41
7

This is an authentication issue. In my case, it solved by below steps: 1- Go to IIS manager, in the left pane, expand the server root and select your web application from Sites node. 2- In the Home screen, go to IIS section and select Authentication. 3- Enable Anonymous Authentication. 4- Then, select Edit and set Edit Anonymous Authentication Credentials to Application pool identity.

Anonymous Authentication Credentials

6

Add this to your web.config

<location path="Images">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>
Mathias F
  • 15,906
  • 22
  • 89
  • 159
6

Use this in configuration section of your web.config file:

<location path="images">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>
</location>
<location path="css">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>
</location>
<location path="js">
<system.web>
  <authorization>
    <allow users="*"/>
  </authorization>
</system.web>
</location>
Rohit Vyas
  • 1,949
  • 3
  • 19
  • 28
4

This issue occurs when IIS does not render the static contents like your JS, CSS, Image files.

To resolve this issue, you need to follow the below steps:

GO to Control Panel > Turn Windows features on or off > Internet Information Services > World Wide Web Services > Common HTTP Features > Static Content.

Ensure that static content is turned on.

Bingo. And you are done. Hard-reload the page and you will be able to see all the static contents.

3

For me adding this in the web.config resolved the problem

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" >
      <remove name="UrlRoutingModule"/>    
    </modules>
</system.webServer>
Tunaki
  • 132,869
  • 46
  • 340
  • 423
ihebiheb
  • 3,673
  • 3
  • 46
  • 55
2

My hour of pain was due to defining MIME types in the web.config. I needed this for the development server but local IIS hated it because it duplicated MIME types... once I removed these from the web.config the problem with js, css, and images not loading went away.

peroija
  • 1,982
  • 4
  • 21
  • 37
  • Hard to believe it can be duplicate MIME types! I scoured for at least an hour trying different user permissions, etc etc but it ended up being duplicated MIMEs in my web.config :-| – Aaron Hudon Oct 06 '17 at 03:14
1

In my case,

IIS can load everything with localhost, but could not load my template files app.tag from 192.168.0.123

because the .tag extension was not in the list.

IIS MIME types

Hiep
  • 2,483
  • 1
  • 25
  • 32
1

To fix this:

Go to Internet Information Service (IIS)

Click on your website you are trying to load image on

Under IIS section, Open the Authentication menu and Enable Windows Authentication as well.

1

One suggestion I have found helpful in the past when developing sites in localhost test environment when working with a copy of production site. Make sure that you comment out the canonical tags:

  <!--<base href="http://www.example.com/">//-->
yardpenalty.com
  • 1,244
  • 2
  • 17
  • 32
1

If you tried all the above solutions and still have problems, consider using the method ResolveClientUrl() of ASP.NET .

A script for Example :

Instead of using

<script src="~/dist/js/app.min.js" ></script>

Use the method

<script src="<%= ResolveClientUrl("~/dist/js/app.min.js") %>" ></script>

This was my solution that worked for a friend I was helping!

KeitelDOG
  • 4,750
  • 4
  • 18
  • 33
1

I had this same problem. For me, it was due to Cache-Control header being set at the server level in IIS to no-cache, no-store. So for my application I had to add in the below to my web.config:

<httpProtocol>
    <customHeaders>
        <remove name="Cache-Control" />
    </customHeaders>
</httpProtocol>
Daniel Cox
  • 11
  • 2
1

I had the same problem but while migrating my ASP.NET MVC5 legacy (has is) from

an old Windows Server 2012 R2 to a Windows Server 2022.

I my case (thanks to @Hiep on this thread) i found out what the problem was...

My old IIS didn't support (by default) ".woff" MIME type so i manualy added it to web.config in specific section like so :

<staticContent>
   <mimeMap fileExtension=".woff2" mimeType="application/font-woff2"/>
</staticContent>

When i try to see "MIME Types" on IIS for that site it will give strange config error and load that "MIME Types" area of IIS clean with no MIME Types (zero)

After i removed that code from my web.config "magically" all MIME Types where correctly loaded on the respective IIS section and site started working as expected.

Thanks Hiep.

João Sa
  • 11
  • 2
0

One possible cause of this is that your application expects to run on port 443 (standard SSL port) and port 443 is already in use. I have run into this several times with developers trying to run our application while Skype is running on their computers.

Incredibly, Skype runs on port 443. This is a horrible design flaw in my opinion. If you see your application trying to run on 444 instead of 443, shut down Skype and the problem will go away.

Robert MacGrogan
  • 357
  • 2
  • 12
0

I added app.UseStaticFiles(); this code in starup.cs of Configure method, than it is fixed.

And Check your permission on this folder.

Metin Atalay
  • 1,375
  • 18
  • 28
0

For Images use

@Url.Content("~/assets/bg4.jpg")

on a Style use this

style="background-image:url(@Url.Content("~/assets/bg4.jpg"))
0

In my case when none of my javascript, png, or css files were getting loaded I tried most of the answers above and none seemed to do the trick.

I finally found "Request Filters", and actually had to add .js, .png, .css as an enabled/accepted file type.

Once I made this change, all files were being served properly.

Merr Leader
  • 745
  • 2
  • 10
  • 13
0

If you're seeing 403 errors in your browser console, check your MVC Bundle Config. Bundle names should not match any existing folder names in your project.

eg.

bundles.Add(new StyleBundle("~/Content/css")...

...would cause issues for IIS if the folder structure $(ProjectDir)\Content\css exists in your project, since it tries to look within the existing folder for bundle content that's not there.

Instead just use something like:

bundles.Add(new StyleBundle("~/Content/cssbundle")...
ShhTot
  • 75
  • 8
0

I had exact same problem, tried all of the suggestions but got nothing. Bugged me hours. Turned out my ISP blocked port 80 for some reasons.

Advice: check your traffic. Make sure port 80 works both locally (i.e. firewall) and externally.

wahaha
  • 915
  • 1
  • 6
  • 9
0

for me this solved my problem

  1. To Remove the HttpNotFoundHandler tag from the handlers section of web.config to allow All file types

OR

  1. To Add StaticFileHandler tags to allow my favorite file types (to avoid 404 error)
  <system.webServer>
    <handlers>

      <!-- ADD THESE -->

      <add name="MyContentHandler"  path="*.css" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />
      <add name="MyContentHandler2" path="*.png" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />
      <add name="MyContentHandler3" path="*.jpg" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />
      <add name="MyContentHandler4" path="*.js"  verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />

      <!-- OR REMOVE THESE -->

      <remove name="MyBlockViewHandler" />
      <add name="MyBlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>