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

ajbeaven picture ajbeaven · Feb 22, 2012 · Viewed 12.1k times · Source

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?

Answer

bzlm picture bzlm · Feb 22, 2012

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.