Introduction:

Here we will explain to what is Routing in mvc asp.net .how to use Routing in mvc asp.net or how to create Route table in mvc asp.net or how to make use friendly URL by using Routing or how to map incoming request's URL pattern to the URL patterns defined in the Route table with example.  


Description:

Routing is a pattern that maps to the incoming request and figure out what to do with that request. At runtime, Routing engine use the Route table for map the incoming request's URL pattern to the URL patterns defined in the Route table. You can register more than one URL patterns to the Route table. 


RouteConfig

public static void RegisterRoutes(RouteCollection routes)

        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

           routes.MapRoute(

                name: "Default",

                url: "{controller}/{action}/{id}",

                defaults: new { controller = "Home", action = "Index",

id = UrlParameter.Optional }

            );

        }


Global.asax


protected void Application_Start()

  {

    RouteConfig.RegisterRoutes(RouteTable.Routes);

  }


When the routing engine finds a match in the route table for the incoming request's URL, it forwards the request to the associated 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 across the entire application. Route name can’t be duplicate.

What is Routing in Mvc



In above example I 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 give the values for controller or action or id defined in the pattern then these values will be fulfill  by the routing system.

Suppose your web application is running on www.Demo.com then the url pattren for you application will be www.Demo.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.


Request URL’s

Parameters

http://demo.com

controller=Home, action=Index, id=none

http://demo.com/Admin

controller=Admin, action=Index, id=none

http://demo.com/Admin/article

controller=Admin, action=article, id=none

http://demo.com/Admin/article/1

controller=Admin, action=article, id=1

http://demo.com/Admin/article/mvc/1

No Match Found


Thanks.


0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like