Routing Requests to Controller Actions

Controller Actions
Routing Requests to Controller Actions

In an ASP.NET MVC application, the controller action to be executed in response to a user request is determined by the Uniform Resource Locator (URL) of the incoming request.

The URL is a widely used user interface for the Web. However, not enough attention is paid to the user-friendliness of URLs, and we often find long and complicated URLs, such as: category=laptop&manufacturer=dell&id=1243&color=black

Observe the preceding URL and you can understand that the website has a directory structure that contains a product folder and products.aspx file within that folder.

This requires a direct relationship between the URL and the physical files stored on the disk.

After receiving this URL request, the Web server directs the request to the products.aspx file, which contains the code and markup to render a response to the browser.

The products.aspx file uses the data in the query string to identify the type of content to display.

In the preceding example, there is a one-to-one mapping between the URLs and the file system. However, this is usually not the case in an MVC application.

In an MVC application, whenever a user request is generated, the URL of the incoming request is routed to a controller action.

The mechanism for locating an appropriate action method for a given URL is called routing. Routing in the ASP.NET MVC framework serves the following two
main purposes:

1.It maps the URLs of incoming requests with controller actions.
2. It builds outgoing URLs that correspond to controller actions.

In an ASP.NET MVC application, because the URLs do not need to map with files, the URLs can be more descriptive of the user’s action and do not need to use long and complicated query strings. For example, consider the following URL: dell/1243/black

The preceding URL points to the products() action method of the product controller actions. The routing mechanism passes the values, laptop, dell, 1243, and
black, to the action method that the URL points to.

The action method can use these values to determine the content to be displayed. A user can easily modify the preceding URL to navigate to another page of the site.

In addition, one important benefit of this kind of URL is Search Engine Optimization (SEO). SEO is the process of optimizing a URL for a search engine to improve the page ranking in search engine results.

SEO reflects what people search for by using the keywords typed into the search engine and how search engines work to process these requests.

Search engines mainly work using URLs. Specifying a meaningful and more comprehensive URL is critical to make the application more search engine-friendly.

An ASP.NET MVC application defines the default route for mapping URLs in a given pattern with specific controller actions. However, you can define additional routes if there is a need to provide custom routes.

The Default Route

Every MVC application needs at least one route to define how the application should handle user requests. Whenever you create an ASP.NET MVC application, the application, by default, generates a route for routing user requests.

The Global.asax file has a method, Application_Start(), defined in it. This is the first method that is invoked when an MVC application starts.

Finally, this method invokes the RegisterRoutes() static method, which is defined in the RouteConfig.cs file in the App_Start folder.

The RegisterRoute() method has the definition of the default route, as shown in the following code snippet:

name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”,
action = “Index”, id =
UrlParameter.Optional }

The preceding code snippet defines a route named Default. This route defines a URL pattern, {controller}/{action}/{id}, with three segments: controller, action, and id. In addition, it defines the default value for each segment in the pattern.

The default value of the controller actions segment is defined as Home, the action segment is Index, and it is an optional parameter.

Therefore, if a URL does not specify an action method, the request is routed to the default action method, Index(), in the specified controller actions.

Similarly, if a URL request does not specify a controller actions, the request is routed to the Index() action method of the Home controller.

The following table shows the requested URLs and the controllers, actions, and ID parameters that they map with, as per the default routing scheme:

Controller Actions
The URL Mapping with Controller Actions as per the Default
Routing Scheme


Please enter your comment!
Please enter your name here