ASP.NET MVC:CSS文件存在时返回302错误

ajb*_*ven 9 iis asp.net-mvc routing http-status-code-302

今天早上我在localhost的ASP.NET MVC 2站点上的一个CSS文件上返回302错误,我不知道会有什么改变导致这个.

localhost站点使用IIS 7.5,虽然我对IIS的经验有限,所以我没有太多关注那里可能发生的事情.

CSS文件的URL是:

http://localhost/MySite/Content/Site.css?v=16
Run Code Online (Sandbox Code Playgroud)

并且响应上的位置标头如下所示:

/MySite/Account/Login?ReturnUrl=%MySite%2fContent%2fSite.css%3fv%3d16&v=16
Run Code Online (Sandbox Code Playgroud)

这让我觉得MVC正在重定向静态文件或类似的东西,但是如果是这种情况,那么我希望我的所有图像,CSS和JavaScript文件都做同样的事情.以防万一,这是RegisterRoutes()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" }
    );
}
Run Code Online (Sandbox Code Playgroud)

此外,如果我将我的CSS文件的名称更改为Site2.css并引用它,则会发生同样的事情.

这是怎么回事?

bzl*_*zlm 10

重定向到登录方法使得它看起来像是因为对目录或文件的权限而不是捕获它的MVC路由.(如果它被MVC路由捕获,则可能会导致确定使用哪个控制器和/或操作时出错.)

ASP.NET MVC本身只保留静态文件,但如果ASP.NET通常决定匿名用户无法访问CSS文件或其目录,ASP.NET将重定向到登录URL,这将是ASP.NET MVC操作.


gba*_*ill 9

看起来web.config中的授权规则说您必须通过身份验证才能看到css页面.您应该能够通过登录并查看是否可以正确提供css文件来证明这一点.

我将向web.config添加一个位置部分,以删除内容目录上的授权要求.摘自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>
Run Code Online (Sandbox Code Playgroud)