Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm getting a 302 error returning on a single CSS file on an ASP.NET MVC 2 site in localhost this morning and I don't know what would have changed to cause this.

The localhost site uses IIS 7.5, though I've had limited experience with IIS so I haven't looked to much in to what could be going on there.

The URL to the CSS file is:

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

and the location header on the response looks like this:

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

This makes me think that MVC is redirecting the static file or something like that, however if that was the case, then I would expect all my images, CSS and JavaScript files to be doing the same which they're not. 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 reference that instead, the same thing happens.

What's going on?

like image 215
ajbeaven Avatar asked Feb 22 '12 20:02

ajbeaven


People also ask

What causes a 302 redirect?

What is an HTTP 302? The 302 status code is a redirection message that occurs when a resource or page you're attempting to load has been temporarily moved to a different location.

What is 302 not found?

A 302 status code is HTTP response status code indicating that the requested resource has been temporarily moved to a different URI.


2 Answers

The redirect to the logon method makes it look like this is because of permissions on the directory or the file rather than an MVC route catching it. (If it were caught by an MVC route, it would probably rather result in an error determining which controller and/or action to use.)

ASP.NET MVC itself leaves static files alone, but if ASP.NET in general decides that the anonymous user doesn't have access to the CSS file or its directory, ASP.NET will redirect to the log-on URL, which will be an ASP.NET MVC action.

like image 75
bzlm Avatar answered Oct 29 '22 05:10

bzlm


Looks like the authorization rules in the web.config are saying that you have to be authenticated to see the css pages. You should be able to prove that by logging in and seeing if you can get the css file to be served correctly.

I'd add a location section to the web.config to remove the authorization requirement on 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>
like image 42
gbanfill Avatar answered Oct 29 '22 06:10

gbanfill