ASP.NET MVC Routing with a controller named “Prope

2019-04-26 22:42发布

问题:

I'm having a tricky issue (bear with me as I'm new to MVC) with trying to use a controller (and a route subsequently) with the name PropertiesController.

I believe this is because there is a directory (which I can't really remove) called "Properties" in my solution. Is there a way round this?

The route setup is just one simple route:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Properties", action = "List", id = "" } // Parameter defaults 
);

and the error I get in IIS7 when requesting "http://localhost/aptment2/properties/" is:

Surely there is a way round this that I just can't find? Cheers.

回答1:

Since someone else is having the same problem, I'm guessing that you're running into a reserved keyword here (I imagine ClassControllers are out as well).

It's not the first time it's happened either - digging deeper into the internals of the environment, you can't route to Windows' reserved keywords either.



回答2:

I have a PropertiesController setup on a MVC 3 application configured to use IIS Express. I don't get the exact error that you do, but it is similar (404.20 no default document) and I'm sure it is for the same reason.

It doesn't cause a problem at all in our production environment since we publish a compiled build of the app and then it doesn't have a Properties folder.

To get around the issue while developing locally I've got all my static content (scripts/css/etc...) in a single directory and I ignore it while also turning on RouteExistingFiles.

  routes.IgnoreRoute("static/{*wildcard}");
  routes.RouteExistingFiles = true;

This fixed my issue and now I can browse example.com/properties/ error free.



回答3:

Running a quick test, I get the same behavior. Is it a viable option to use something other than Properties?



回答4:

I don't know if it's an option for you, but you could try to disable routing to existing files.

routes.RouteExistingFiles = false;