CSS, Images, JS not loading in IIS

C#asp.net MvcIis

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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?

Solution 3 - C#

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

Solution 4 - C#

Try removing the staticContent section from your web.config.

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

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

Add this to your web.config

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

Solution 9 - C#

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>

Solution 10 - C#

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

Solution 11 - C#

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.

Solution 12 - C#

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

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

Solution 13 - C#

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.

Solution 14 - C#

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

Solution 15 - C#

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.

Solution 16 - C#

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

Solution 17 - C#

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!

Solution 18 - C#

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>

Solution 19 - C#

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.

Solution 20 - C#

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

And Check your permission on this folder.

Solution 21 - C#

For Images use

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

on a Style use this

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

Solution 22 - C#

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.

Solution 23 - C#

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

Solution 24 - C#

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.

Solution 25 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionImran RashidView Question on Stackoverflow
Solution 1 - C#Matt KocajView Answer on Stackoverflow
Solution 2 - C#Moiz TankiwalaView Answer on Stackoverflow
Solution 3 - C#OgglasView Answer on Stackoverflow
Solution 4 - C#intrepidisView Answer on Stackoverflow
Solution 5 - C#user23462View Answer on Stackoverflow
Solution 6 - C#Tim M.View Answer on Stackoverflow
Solution 7 - C#Imran RashidView Answer on Stackoverflow
Solution 8 - C#Mathias FView Answer on Stackoverflow
Solution 9 - C#Rohit VyasView Answer on Stackoverflow
Solution 10 - C#Alireza AbdollahnejadView Answer on Stackoverflow
Solution 11 - C#Aman AgarwalView Answer on Stackoverflow
Solution 12 - C#ihebihebView Answer on Stackoverflow
Solution 13 - C#peroijaView Answer on Stackoverflow
Solution 14 - C#HiepView Answer on Stackoverflow
Solution 15 - C#NaijalivemediaView Answer on Stackoverflow
Solution 16 - C#yardpenalty.comView Answer on Stackoverflow
Solution 17 - C#KeitelDOGView Answer on Stackoverflow
Solution 18 - C#Daniel CoxView Answer on Stackoverflow
Solution 19 - C#Robert MacGroganView Answer on Stackoverflow
Solution 20 - C#Metin AtalayView Answer on Stackoverflow
Solution 21 - C#Maximiliano CesánView Answer on Stackoverflow
Solution 22 - C#Merr LeaderView Answer on Stackoverflow
Solution 23 - C#ShhTotView Answer on Stackoverflow
Solution 24 - C#wahahaView Answer on Stackoverflow
Solution 25 - C#João SaView Answer on Stackoverflow