ASP.NET MVC:CSS 文件存在时返回 302 错误
今天早上,我在 localhost 的 ASP.NET MVC 2 站点上的单个 CSS 文件上返回了 302 错误,我不知道会发生什么变化导致这种情况。
本地主机站点使用 IIS 7.5,尽管我对 IIS 的经验有限,所以我没有过多研究那里可能发生的情况。
CSS 文件的 URL 是:
http://localhost/MySite/Content/Site.css?v=16
响应上的位置标头如下所示:
/MySite/Account/Login?ReturnUrl=%MySite%2fContent%2fSite.css%3fv%3d16&v=16
这让我认为 MVC 正在重定向静态文件或类似的内容,但是如果是这样的话,那么我会期望我的所有图像, CSS 和 JavaScript 文件所做的事情与它们不做的事情相同。以防万一,这里是 Global.ascx 中 RegisterRoutes()
的简化版本:
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" }
);
}
另外,如果我将 CSS 文件的名称更改为 Site2.css 并引用它,也会发生同样的情况。
这是怎么回事?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
登录方法的重定向看起来像是因为目录或文件的权限,而不是捕获它的 MVC 路由。 (如果它被 MVC 路由捕获,则可能会导致确定要使用哪个控制器和/或操作的错误。)
ASP.NET MVC 本身不会处理静态文件,但如果 ASP.NET 通常决定匿名用户无权访问 CSS 文件或其目录,ASP.NET 将重定向到登录 URL,这将是 ASP.NET MVC 操作。
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.
看起来 web.config 中的授权规则表明您必须经过身份验证才能查看 css 页面。您应该能够通过登录并查看是否可以正确提供 css 文件来证明这一点。
我将在 web.config 中添加一个位置部分,以删除内容目录上的授权要求。摘自http://support.microsoft.com/kb/316871
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