Routing Concepts in Asp.Net MVC

MVC Routing Concept: MVC Routing is used to eliminate needs of mapping each URL with a physical file. Routing enable us to define URL pattern that maps to the request handler. This request handler can be a file or class.
Each MVC application has at least one default route.

MVC Routing
MVC Routing

ngConfig.png” alt=”Route Configuration” width=”911″ height=”425″ /> Route Configuration[/caption]

In the above example route config will handle different routing URL pattern with same controller and same method.

e.g.    https://myapplication/Invoice/Index

https://myapplication/Proposal/Index

Both URL hit the same controller and same method as per route configuration but URL is different.

Default“, url pattern is “{controller}/{action}/{id}” and defaults parameter for controller, action method and id parameter. where id is set to optional parameter.

so there may be two url pointing the same method.

  1. https://myapplication/Invoice/Index
  2. https://myapplication/Invoice/Index/10

Here first url showing all Invoices through index method. and Second URL showing only one invoice having invoiceid is 10. with same method.

 

Route Constraint: We can also apply restrictions on the value of parameter by configuring route constraints. e.g. need to id parameter value is numeric only. we can do as in code.

this will allow only numeric value in id parameters.

Finally when we define all things we have to registered in Global.asax under application_Start event.

Will keep you posted more about Routing in MVC..