152

All my applications were working fine, but suddenly, all the sites under IIS are not loading css, images, scripts and redirect to the 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.

6
  • wrong tagging, not real question... Commented May 9, 2012 at 8: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. Commented May 9, 2012 at 8: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 Commented May 9, 2012 at 9:01
  • @ImranRashid - is this site inheriting settings from another site in IIS? Commented May 9, 2012 at 9:17
  • @Tim Medora - No sir it is not inheriting from other site. Commented May 9, 2012 at 9:26

28 Answers 28

225

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

Original source: http://adilmughal.com/blog/2011/11/iis-7-not-loading-css-and-image/, now available at the Wayback Machine

Windows 10:

windows 10 version of the above dialog

Sign up to request clarification or add additional context in comments.

12 Comments

I had a similar issue, realised it was an installation problem, but enabling the static content seems to have fixed it!
This saved my bacon, you would think this was enabled by default but on windows 10 it isn't.
Lame this isn't checked by default.
@RayLoveless I think what's lame is that MVC takes a hard dependency on this IIS feature being enabled for static content to work in MVC. I think they shouldn't be so coupled. Maybe .NET Core will fix this. :p
Aaand here we are again, being saved by the same answer, 2 years after from the last time.
|
219

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?

6 Comments

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.
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.
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.
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.
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!
|
23

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/).

1 Comment

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
20

Try removing the staticContent section from your web.config.

<system.webServer>
    <staticContent>
        ...
    </staticContent>
</system.webServer>

4 Comments

Removing staticContent works but now all my fonts are 404?
I'm not an expert at this, but perhaps you need to add the correct MIME type declarations for your fonts via IIS Manager in the HTTP Headers tab of the website properties.
This solved the problem for me, because I have some .woff file entries there in development, but on production same setup needs adjustment
Thanks intrepidis, in my case your suggestion worked for me. Cheers!
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.

1 Comment

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.
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.

3 Comments

authentication mode is Forms not window? application web config file override machine config ? Is there any setting in IIS which is causing this?
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.
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
8

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.

4 Comments

I had the same problem, Just moved the project files from desktop to wwwroot folder, additionally I have added full control to network service, which is related to the application pool.
Hi Imran/Mihkov, can you please help me on this?
Hi @MohanrajPeriyannan, answer has the solution? you still unable to resolve the problem using above solution?
Hi @ImranRashid, I couldn't understand the solution. Can you please share a demo or screenshot for this. Thanks for your reply.
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

Comments

6

Add this to your web.config

<location path="Images">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>

1 Comment

The path is relative, sometimes adding the root will resolve this. Try setting the path to "~/Images"
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>

Comments

5

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.

Comments

3

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

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" >
      <remove name="UrlRoutingModule"/>    
    </modules>
</system.webServer>

Comments

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.

1 Comment

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 :-|
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

Comments

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.

Comments

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/">//-->

Comments

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!

Comments

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>

Comments

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.

Comments

1

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>

Comments

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.

Comments

0

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

And Check your permission on this folder.

Comments

0

For Images use

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

on a Style use this

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

Comments

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.

Comments

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")...

Comments

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.

Comments

0

I was able to follow these instructions: https://serverfault.com/a/1063414/402955

There is a Connect as... setting in IIS that can also be configured. I set both my Application Pool Identity to the user account, and also set the Connect as... settings to that same account on the site itself.

Connect as IIS settings

Comments

0

Migration from IIS 8.5 to IIS 10, had same issue. Eventually changed to this

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

and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.