Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC route mapping

I'm new to MVC (and ASP.Net routing). I'm trying to map *.aspx to a controller called PageController.

routes.MapRoute(
   "Page",
   "{name}.aspx",
   new { controller = "Page", action = "Index", id = "" }
);

Wouldn't the code above map *.aspx to PageController? When I run this and type in any .aspx page I get the following error:

The controller for path '/Page.aspx' could not be found or it does not implement the IController interface. Parameter name: controllerType

Is there something I'm not doing here?

like image 587
Ryan Eastabrook Avatar asked Aug 15 '08 03:08

Ryan Eastabrook


People also ask

What is map route 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. The RouteConfig. cs file is used to set routing for the application.

Where is route mapping code in ASP.NET MVC?

Where is the route mapping code written? The route mapping code is written in "RouteConfig. cs" file and registered using "global. asax" application start event.

How can use routing in ASP.NET MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).

Which is the correct default route mapping within MVC?

The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home. action = Index.


1 Answers

I just answered my own question. I had the routes backwards (Default was above page).

Yeah, you have to put all custom routes above the Default route.

So this brings up the next question... how does the "Default" route match (I assume they use regular expressions here) the "Page" route?

The Default route matches based on what we call Convention over Configuration. Scott Guthrie explains it well in his first blog post on ASP.NET MVC. I recommend that you read through it and also his other posts. Keep in mind that these were posted based on the first CTP and the framework has changed. You can also find web cast on ASP.NET MVC on the asp.net site by Scott Hanselman.

  • http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
  • http://www.asp.net/MVC/
like image 164
Dale Ragan Avatar answered Sep 20 '22 13:09

Dale Ragan