Posted By : Murugan Andezuthu Dharmaratnam
Posted On : 22 January 2015
Keywords : Routing, MVC Routing, ASP .NET MVC Routing, ASP .NET MVC, MVC

MVC Routing

Routing helps us to use URLs that do not have to map to a specific files.In web application when type a url for example ideally you are Mapping your URL to a specific file stored in your Webserver. if the URLs are not map to a specific files then we cann't use that URLs,in that time routing enable us to use the URLs.Route is URL pattern and is mapped to a handler,handler can be a class or controller.

Routing And URL Rewriting

These two are different approaches,but both of these approches helps to make a seperation between incoming URLs and what ends up handling the request.URL writing process the incoming requests by changing the URLs before it sends the request to the webpage,ie it actually mapping one URL to another.URL Patterns are used in case of URL rewritting because it doesn't have any API for creating URLs,in this case if we change the URL pattern we must manually update all hyperlinks that contain original URL.

In case of routing,values are extract from URLs so the URL is not changed when a request is handled.If we need to create a URL,simply pass parameter values into a method that generates the URL.Route determines how the request is dispatched based on the characteristics of the URL,ie it doesn't rewrite the URL.Routing also helps generate URLs using the same mapping rules that it uses to match incoming URLs.

What Is Routes?

Routes are actually the URL patterns.All MVC application needs a route that describes how the application handles a request.Different application consists of different number of routes,ie depends on the complexity.Routes specify some default values and constraints.In route a placeholder is defined by enclosing them in braces,also we can define more than one placeholder value between delimiters but they must seperated by a constant value.

We add routes in a method that is called from the handler for the Application_Start event in the Global.asax file. This approach makes sure that the routes are available when the application starts. It also enables you to call the method directly when you unit-test the application.Example shows How to: Use Routing with Web Forms.

How To Add Constraint To The Route?

Constraints are the rule enforced on the parameter value of the URL. If the parameter values are not inline with the constraint for a route is not handled by the URL routing engine. You can add the constraint to the URL route based on your application requirement.In addition to matching a URL request to a route definition by the number of parameters in the URL, you can specify that values in the parameters meet certain constraints.
Constraints are defined by using regular expressions or by using objects that implement the IRouteConstraint interface. When you add the route definition to the Routes collection, you add constraints by creating a RouteValueDictionary object that contains the verification test. You then assign this object to the Constraints property. The regular expression is always treated as case-insensitive.

How To Create Custom Route?

All the mapped routes are stored as RouteCollection in the Routes property of the RouteTable class.URL requests can be easily matched to action methods in controller classes using the default route. If your application, however, needs to accept URL requests with a different structure, then you can easily create new routes by using the MapRoute() extension method in the RouteConfig class.

Murugan Andezuthu Dharmaratnam

Murugan is a technology leader with 14 years of hands-on experience in full life cycle systems design, development, implementation, and management. In addition to systems design and development, his core competencies include cloud design and architecture, interactive web applications and websites development, mobile computing, and Healthcare Information Technology. His experience is very diverse with a broad range of technologies within multiple industry settings including private, public, and non-profit. Coupled with his technology expertise, Murugan is a well-organized, result-oriented, and a highly analytical leader with proven track record in developing and implementing very complex systems.