ASP.NET MVC: CSS file returning 302 error when it exists

I am getting error 302 in a single CSS file on an ASP.NET MVC 2 site on a local host this morning, and I don’t know what would change to cause this.

The localhost site uses IIS 7.5, although I had limited experience with IIS, so I did not look at much that could happen there.

CSS file url:

http://localhost/MySite/Content/Site.css?v=16 

and the location header in the response is as follows:

 /MySite/Account/Login?ReturnUrl=%MySite%2fContent%2fSite.css%3fv%3d16&v=16 

This makes me think that MVC is redirecting a static file or something like that, however, if that were the case, I would expect all my images, CSS and JavaScript files to do the same thing they did. Just in case, here is a simplified version of RegisterRoutes() in Global.ascx:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute("", "Account/{action}/", new { controller = "Account" }); routes.MapRoute("", "{action}", new { controller = "Home", action = "Index" }); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "Error", "{*url}", new { controller = "Home", action = "ResourceNotFound" } ); } 

Also, if I change the name of my CSS file to Site2.css and refer to it, the same thing will happen.

What's happening?

+7
source share
2 answers

Redirecting to the login method makes it look like it is related to access rights to the directory or file, and not to the MVC trace. (If it was caught using the MVC route, this will most likely result in an error determining which controller and / or action to use.)

ASP.NET MVC itself leaves the static files alone, but if ASP.NET even decides that the anonymous user does not have access to the CSS file or its directory, ASP.NET will redirect the login URL, which will be the action of ASP.NET MVC .

+8
source

It seems that the authorization rules in web.config say that you need to be authenticated in order to view the css pages. You should be able to prove that by logging in and seeing if you can properly load the css file.

I would add a location section in web.config to remove the authorization request in the content directory. Taken from http://support.microsoft.com/kb/316871

 <!-- This section gives the unauthenticated user access to all of the files that are stored in the Content folder. --> <location path="content"> <system.web> <authorization> <allow users ="*" /> </authorization> </system.web> </location> 
+8
source

All Articles