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.
-
3Just avoid using the same names, now you're getting into unnecessary problems that could be avoided other ways. Just rename you Content folder to public or something similar.mare– mare2011-07-10 19:16:10 +00:00Commented Jul 10, 2011 at 19:16
-
2Well, I know that. But my question is a general case. What if you encounter a real name collision? Anyway, thank you for mentioning.Saeed Neamati– Saeed Neamati2011-07-10 19:17:57 +00:00Commented Jul 10, 2011 at 19:17
Add a comment
|
2 Answers
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 :-)
Comments
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
);
2 Comments
Saeed Neamati
Downvote, because I said that I want to rewrite the incoming URL before it maps to route rules. I already have those route rules in action. But IIS StaticHandler takes precedence in responding.
Mrchief
How about mentioning that in the post? Sorry, my mind scanner is not working today.