Routing helps you to define user friendly URL structure and map those URL structure to the controller.
For instance let's say we want that when any user types "http://localhost/View/ViewCustomer/" , it goes to the "Customer" Controller and invokes "DisplayCustomer" action.This is defined by adding an entry in to the "routes" collection using the "maproute" function. Below is the under lined code which shows how the URL structure and mapping with controller and action is defined.

The route mapping code is written in "RouteConfig.cs" file and registered using "global.asax" application start event.
Yes , you can , you just need to make two entries with different key names and specify the same controller and action.
Explain attribute based routing in MVC?
This is a feature introduced in MVC 5. By using the "Route" attribute we can define the URL structure. For example in the below code we have decorated the "GotoAbout" action with the route attribute. The route attribute says that the "GotoAbout" can be invoked using the URL structure "Users/about".
What is the advantage of defining route structures in the code?
Most of the time developers code in the action methods. Developers can see the URL structure right upfront rather than going to the "routeconfig.cs" and see the lengthy codes. For instance in the below code the developer can see right upfront that the "GotoAbout" action can be invoked by four different URL structure.
This is much user friendly as compared to scrolling through the "routeconfig.cs" file and going through the length line of code to figure out which URL structure is mapped to which action


Comments
Post a Comment