Wednesday, March 18, 2015

ASP.NET MVC: Routing Overview

Basically, Routing is a pattern matching system that monitor the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for matching the incoming request's URL pattern against the URL patterns defined in the Route table. You can register one or more URL patterns to the Route table at Application_Start event. MVC5 also supports attribute routing, to know more refer Attribute Routing in ASP.NET MVC.

How to define route...

  1. public static void RegisterRoutes(RouteCollection routes)
  2. {
  3. routes.MapRoute(
  4. "Default", // Route name
  5. "{controller}/{action}/{id}", // Route Pattern
  6. new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Default values for above defined parameters
  7. );
  8. }
  9.  
  10. protected void Application_Start()
  11. {
  12. RegisterRoutes(RouteTable.Routes);
  13. //To:DO
  14. }
When the routing engine finds a match in the route table for the incoming request's URL, it forwards the request to the appropriate controller and action. If there is no match in the route table for the incoming request's URL, it returns a 404 HTTP status code.

Note

Always remember route name should be unique across the entire application. Route name can’t be duplicate.

How it works...

In above example we have defined the Route Pattern {controller}/{action}/{id} and also provide the default values for controller,action and id parameters. Default values means if you will not provide the values for controller or action or id defined in the pattern then these values will be serve by the routing system.
Suppose your webapplication is running on www.example.com then the url pattren for you application will be www.example.com/{controller}/{action}/{id}. Hence you need to provide the controller name followed by action name and id if it is required. If you will not provide any of the value then default values of these parameters will be provided by the routing system. Here is a list of URLs that match and don't match this route pattern.
Matching URLs
Request URL                                     Parameters
http://example.com/                      controller=Home, action=Index, id=none, Since default value of controller and action are Home and Index respectively.

http://example.com/Admin         controller=Admin, action=Index, id=none, Since default value of action is Index

http://example.com/Admin/Product   controller=Admin, action=Product, id=none

http://example.com/Admin/Product/1  controller=Admin, action=Product, id=1

http://example.com/Admin/Product/SubAdmin/1 No Match Found

http://example.com/Admin/Product/SubAdmin/Add/1 No Match Found

Examples of Valid Route Patterns in ASP.NET MVC
Route Pattern URL Example
mysite/{username}/{action} ~/mysite/jatten/login
public/blog/{controller}-{action}/{postId} ~/public/blog/posts-show/123
{country}-{lang}/{controller}/{action}/{id} ~/us-en/products/show/123
products/buy/{productId}-{productName} ~/products/but/2145-widgets

Difference between Routing and URL Rewriting

Many developers compare routing to URL rewriting that is wrong. Since both the approaches are very much different. Moreover, both the approaches can be used to make SEO friendly URLs. Below is the main difference between these two approaches.

  1. URL rewriting is focused on mapping one URL (new url) to another URL (old url) while routing is focused on mapping a URL to a resource.
  2. Actually, URL rewriting rewrites your old url to new one while routing never rewrite your old url to new one but it map to the original route.
Reference:http://www.dotnet-tricks.com/Tutorial/mvc/HXHK010113-Routing-in-Asp.Net-MVC-with-example.html

No comments:

Post a Comment