55

I'm getting a strange error on my webserver for seemingly every file but the .aspx files.

Here is an example. Just replace '/robots.txt' with any .jpg name or .gif or whatever and you'll get the idea:

The controller for path '/robots.txt' was not found or does not implement 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.

Ben Lesh
  • 107,825
  • 47
  • 247
  • 232

6 Answers6

77

You can ignore robots.txt and all the aspx pages in your routing.

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • I like this solution, it sucks I'm going to have to add one for each file type though. .jpg, .png, .gif, .swf, .pdf, etc etc. – Ben Lesh Jan 21 '10 at 14:30
  • @blesh - are you using a custom `IControllerFactory`? – Daniel A. White Jan 21 '10 at 14:34
  • No... but looking into it, I think I may start. Why do you ask? – Ben Lesh Jan 21 '10 at 14:36
  • @Daniel A White - I started using a custom IControllerFactory as part of my implementation for Unity IoC and this error cropped up (it didn't happen prior). Why did this happen? – Tri Q Tran May 25 '11 at 00:54
  • hmm none of this explains why this error happens in the first place. For some reason I get it only for one image.. I used to get it for .map files and now just for clear.png. why not for any of the other 100 images?! this is driving me nuts.. – Sonic Soul Mar 21 '16 at 17:36
18

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)(/.*)?" });
The Coder
  • 4,981
  • 2
  • 29
  • 36
6

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("Main", "Navigation", new { area = string.Empty })
Daniel
  • 920
  • 1
  • 11
  • 22
3

I found another solution too... 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.
);
Ben Lesh
  • 107,825
  • 47
  • 247
  • 232
2

Do you still have:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

... in your Global.asax.cs?

MVC puts it there by default, and it's supposed to handle this.

If you do, then the problem may be how you're mixing MVC and WebForms.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
1

I encountered this error when I request resources that did not exist.

Specifically, I was requesting a custom IE css file:

<!--[if lt IE 8]>@Styles.Render("~/Content/ie7.css")<![endif]-->

(These are condition comments, interpreted by IE)

However, the actual resource existed on ~/Content/ie/ie7.css.

So, without any modifications to the routing, the error was solved by using the correct url of the resource.

R. Schreurs
  • 8,587
  • 5
  • 43
  • 62
  • 1
    Out of curiosity, why bother with the condition comments if you can detect browser version in a server side @block and just not render the extra styles at all? – mikeschuld Nov 02 '15 at 18:27
  • Well, actually I never bothered. The css and most of the mark-up were the work of a team mate. I suppose its just a result of a front-end designer's wish to keep everything under control. Triggered by your remark, I just read on [MSDN - About conditional comments](https://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx) that support has been dropped in IE10 standards mode, so, we'll have to look into this. – R. Schreurs Nov 02 '15 at 20:35