views:

952

answers:

3

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.

+4  A: 

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
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.
blesh
@blesh - are you using a custom `IControllerFactory`?
Daniel A. White
No... but looking into it, I think I may start. Why do you ask?
blesh
Sometimes these errors do happen if the file doesn't exist.
Daniel A. White
I used this, but the second line (referencing {*robotstxt}) needs to have 'favicon' replaced by 'robotstxt'.
Freewalker
+1  A: 

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.
);
blesh
+1  A: 

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
And make sure it is at the top!
Martin
I have it... but it's below `routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");` is that an issue?
blesh
Shouldn't be a problem.
Craig Stuntz