Saeed Neamati
Saeed Neamati

Reputation: 35842

ASP.NET Controller name conflicted with a folder name

In my ASP.NET MVC3 project, I have a folder called Content (the default folder for an MVC project). But I also have a controller called Content. And when I want to use the default actions of this controller, I simply use http://domain/content/, which is equivalent to http://domain/content/index. But IIS returns 403 error and thinks that I'm gonna get the directory list of the Content Folder. Well, this question is already discussed in this question. But I don't know how to rewrite my URL to append the default action to it. May someone help please.

Upvotes: 3

Views: 1554

Answers (2)

David Kirkland
David Kirkland

Reputation: 2461

You can get around this by changing routing configuration to specify:

routes.RouteExistingFiles = true;

You will then need to set up some ignore rules to prevent genuine static content being gobbled up by the routing engine.

For example, I have a folder called Touch in my app, and I also have a specific route for Touch. So the working config is:

routes.RouteExistingFiles = true;
routes.IgnoreRoute("Touch/Client/{*touchclientversion}", new { touchclientversion = @"(\d*)(/*)" });

I agree that this kind of thing should generally be avoided, but sometimes it's nice to have pretty URLs :-)

Upvotes: 7

Mrchief
Mrchief

Reputation: 76238

You can add a default route like this:

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

In your case:

routes.MapRoute(
                "DefaultContent",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Content", action = "Index", id = "" }  // Parameter defaults
            );

Upvotes: 0

Related Questions