MVC Routing

by SARNAM SINGH   on Jun 27, 2016   Category: .Net Framework  | Level: Beginner  |  Views: 215    |  Points: 100   
Like this article? Bookmark and Share:
MVC Routing


Routing is a concept , enables you to define URLs map to specific pattern rather than file on web server. In MVC routing , we define url that are descriptive to user action and therefore can easily be understood by user.

 URL pattern can contain literal values and place holders, separated by slash (/) character. So typical url is constructed like <domainname>/{Controller}/{Action}/{parameters}.

 Sometimes urls can contain variable number of parameters as specified in the pattern. so such kind of URL's can be handled by {*} catch all operator. For example ,if we define URL like <domain>/UserOption/{option}/{*} and it will match for all Url's like  useroption/{option},useroption/{option}/1,useroption/{option}/1/2, useroption/{option}/1/2/3 ....etc

  Routing Constraint: It enables you to apply some constraints on parameter values. Keeping in mind the business context, some time its necessary to provide some constraint on parameter value like application expects a parameter is of type int but user supplied it as string or parameer value to be in specific range but its being passed out of that range... etc. So if parameter value does not meet the constraint, that request will not be handled by routing.

 constraint can be applied either by regular expression or by object implementing IRouteConstraint Interface.

Scenario where routing not applied:
   A). Routing does not work, If incoming url matches to the file on the web server. Still, if we want request to be handled by routing then we have to set the RouteExistingFile property  of RouteCollection Object to true.
  B). Routing does not work, If its disabled, not to handle certain type of pattern. Sometimes its preferred to disable routting for certain pattern and that is to be handled by StopRouteHandler object and this can be done by calling IgnoreRoute/Ignore method of RouteColection object , enables StopRouteHandler to handle such request.

Like this article? Bookmark and Share:

Most viewed Articles

User Comments

No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts