Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override directory listing with MVC URL routing

Recently, I partially converted an Asp.Net web forms application to use MVC. We still have parts of the application in web forms (.aspx pages) and use MVC routing to work with Controllers and such. I added an MVC route like

routes.MapRoute("Users", "Users/{controller}/{action}/", new { controller = "Timesheet", action = "List" });

There is a folder called "Users" which contain a few aspx pages we still use. When I hit the URL http://localhost/Users/ I get a directory listing of the contents of the "Users" folder. Apparently, the directory listing takes precedence over MVC url routing and this might be overridden by modifying the IIS7 server settings.

How could I override this behavior, via code or web.config changes?

References:

http://forums.asp.net/t/1251156.aspx/1

http://learn.iis.net/page.aspx/121/iis-7-and-above-modules-overview/

like image 436
prabug Avatar asked Oct 03 '11 17:10

prabug


People also ask

How do I change the default URL in MVC?

How do I change the default page in ASP NET MVC? You can set up a default route: routes. MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults ); Just change the Controller/Action names to your desired default.

What is URL routing in MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller.

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.


1 Answers

Setting RouteExistingFiles=true on the RouteCollection achieves just that. It will allow ASP.NET MVC to handle routes even for existing directories.

like image 191
Kunal Avatar answered Oct 07 '22 01:10

Kunal