asp.net-mvc 我在 MVC2 中的图像和 robots.txt 上收到“未实现 IController”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2109841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I'm getting a "Does not implement IController" error on images and robots.txt in MVC2
提问by Ben Lesh
I'm getting a strange error on my webserver for seemingly every file but the .aspx files.
我的网络服务器上似乎每个文件都出现了一个奇怪的错误,但 .aspx 文件除外。
Here is an example. Just replace '/robots.txt' with any .jpg name or .gif or whatever and you'll get the idea:
这是一个例子。只需将 '/robots.txt' 替换为任何 .jpg 名称或 .gif 或其他名称,您就会明白:
The controller for path '/robots.txt' was not found or does not implement IController.
路径“/robots.txt”的控制器未找到或未实现 IController。
I'm sure it's something to do with how I've setup routing but I'm not sure what exactly I need to do about it.
我确定这与我设置路由的方式有关,但我不确定我到底需要做什么。
Also, this is a mixed MVC and WebForms site, if that makes a difference.
此外,这是一个混合的 MVC 和 WebForms 站点,如果这有所不同的话。
回答by Daniel A. White
You can ignore robots.txt and all the aspx pages in your routing.
您可以忽略 robots.txt 和路由中的所有 aspx 页面。
routes.IgnoreRoute("{*allaspx}", new {allaspx=@".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*robotstxt}", new {robotstxt=@"(.*/)?robots.txt(/.*)?"});
You might want to ignore the favicon too.
您可能也想忽略收藏夹图标。
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"});
You can adjust the regular expression to exclude paths.
您可以调整正则表达式以排除路径。
Haacked from the source.
从源头上攻击。
回答by The Coder
The ignore route given above didn't work for me but I found a similar one that did:
上面给出的忽略路线对我不起作用,但我发现了一个类似的路线:
routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(css|js|gif|jpg)(/.*)?" });
回答by Daniel
This error could also happen if inside a view in your area, you use the Html.Action helper. This helper will always use the area as a prepend, unless you specifically tell it not to. E.g.,
如果在您所在区域的视图中使用 Html.Action 助手,也可能发生此错误。该助手将始终使用该区域作为前置,除非您明确告诉它不要这样做。例如,
@Html.Action("Main", "Navigation", new { area = string.Empty })
回答by Ben Lesh
I found another solutiontoo... While I don't think I'll use it, it's worth showing here in the answers:
我也找到了另一个解决方案......虽然我认为我不会使用它,但值得在答案中展示:
The following should (in theory) ignore looking for controllers for anything with a '.' in it.
以下应该(理论上)忽略寻找任何带有“.”的控制器。在里面。
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = @"[^\.]*" } // Parameter contraints.
);
回答by Craig Stuntz
Do you still have:
你还有没有:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
... in your Global.asax.cs?
...在您的 Global.asax.cs 中?
MVC puts it there by default, and it's supposed to handle this.
MVC 默认将它放在那里,它应该处理这个问题。
If you do, then the problem may be how you're mixing MVC and WebForms.
如果您这样做了,那么问题可能在于您如何混合 MVC 和 WebForms。
回答by R. Schreurs
I encountered this error when I request resources that did not exist.
当我请求不存在的资源时遇到此错误。
Specifically, I was requesting a custom IE css file:
具体来说,我请求自定义 IE css 文件:
<!--[if lt IE 8]>@Styles.Render("~/Content/ie7.css")<![endif]-->
<!--[if lt IE 8]>@Styles.Render("~/Content/ie7.css")<![endif]-->
(These are condition comments, interpreted by IE)
(这些是条件注释,由 IE 解释)
However, the actual resource existed on ~/Content/ie/ie7.css.
但是,实际资源存在于 ~/Content/ie/ie7.css 上。
So, without any modifications to the routing, the error was solved by using the correct url of the resource.
因此,在没有对路由进行任何修改的情况下,通过使用正确的资源 url 解决了错误。

