Routing

Routing is a Pattern Matching system. Routing monitor the Incoming request and find out what to do with that request.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.

Define Route Syntax:


public static void RegisterRoutes(RouteCollection routes)

{

   routes.MapRoute(

   "Default", // Route name
   "{controller}/{action}/{id}", // Route Pattern
    new { controller = "Home", action = "Index", id = UrlParameter.Optional}   //   Default values for above defined parameters
   );
}

protected void Application_Start()
{
     RegisterRoutes(RouteTable.Routes);
 //To:DO
}

"Default" is the route name and it will be register in the route engine.
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.

Route Name should be unique in the application. It cannot be duplicate.                                                                                       
                                   

In the above Syntax Example, I have given the Route Pattern like {controller}/{action}/{id} and also provided the default value for controller="Home", action="index" and id="optional". If you are not provide any default value for controller, action and id then these values will be served by the routing system.
Suppose your web application is www.example.com and the URL pattern is www.example.com/controller/action/id. If you are not provide any value for id, the default value of these parameter will be taken from routing system.

Example:

  1. www.example/controller/action/id = www.example/home/index here i am not given id value. So, it is optiona.
  2.  www.example/controller/action/id = www.example/home/index/1 here i am given id=1 value.


Popular Posts