Overriding a Directory List Using MVC URL Routing

I recently partially converted an Asp.Net web form application to use MVC. We still have parts of the application in web forms (.aspx pages) and use MVC routing to work with controllers, etc. I added an MVC route, for example

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

There is a folder called Users, which contains several aspx pages that we still use. When I click the URL http://localhost/Users/ , I get a list of directories for the contents of the Users folder. Apparently, the directory list takes precedence over the routing of the MVC URL, and this can be overridden by changing the IIS7 server settings.

How can I override this behavior using code changes or web.config?

Literature:

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

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

+8
asp.net-mvc webforms asp.net-mvc-routing
source share
2 answers

Setting RouteExistingFiles = true to RouteCollection allows you to do just that. This will allow ASP.NET MVC to handle routes even for existing directories.

+2
source share

Use this ignoreroute:

 routes.IgnoreRoute("{WebPage}.aspx/{*pathInfo}"); 

List of RegisterRoutes Methods

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{WebPage}.aspx/{*pathInfo}"); //routes.MapPageRoute("users", "users", "~/admin/default.aspx"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "home", action = "index", id = UrlParameter.Optional } // Parameter defaults ); } 

This would exclude all pages whose extension was ".aspx" from routing.

0
source share

All Articles