In this article, I am going to discuss the ideas about Advanced Routing in MVC with examples. Please read our previous articles before moving on to this article where we discussed Routing in ASP.NET MVC. As part of this article, we are going to discuss the following contents which are related to Advanced Routing in MVC.
Contents
Multiple Routes:
Route customization is an advanced topic. In this session, we will look at customize the multiple routing in the MVC application. You can configure the multiple routes using the Map Route extension method in RouteConfig.cs. This method needs at least two parameters such as route name and URL pattern and URL parameter is optional. If you need to pass some value to action using parameters then the parameter is mandatory.
While requested URLs come to the application. It is start compared to route patterns in order the pattern sequence in the route dictionary or order of added the route in RouteConfig.cs file. If request URLs are matched with the first URL pattern of the route dictionary, then it will call the specified controller and action. In case if URL is not matched with a pattern of the route dictionary, then it will call the default map route method and executed the default controller and action. Similarly, you can able to your own route map in routeconfig.cs file. AdvancedRouting in MVC Example -1 Route Configuring: – routeconfig.cs
ASP.NET MVC5 and WEB API 2 supports a new type of routing, called attribute routing. In this routing, attributes are used to define routes. Attribute routing provides you more control over the URIs by defining routes directly on actions and controllers in your ASP.NET MVC application and WEB API.
Enabling Attribute Routing
To enable Attribute Routing, we need to call the MapMvcAttributeRoutes method of the route collection class during configuration. Advanced Routing in MVC
You must have enable attribute route in routeconfig.cs file.
Now we will discuss multiple parameter pass to action. you can define the rules in routeconfig.cs file and also attribute route. For example, the following route applies a multiple parameter pass to action.
Above both examples will work as same. Advanced Routing in MVC
Route Constraints:
It will restrict the value of parameters by configuring route constraints and attribute route constraints. The Route Constraint in ASP.NET MVC Routing allows us to apply a regular expression to a URL segment to restrict whether the route will match the request
Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z)
{x:alpha}
bool
Matches a Boolean value.
{x:bool}
datetime
Matches a DateTime value.
{x:datetime}
decimal
Matches a decimal value.
{x:decimal}
double
Matches a 64-bit floating-point value.
{x:double}
float
Matches a 32-bit floating-point value.
{x:float}
guid
Matches a GUID value.
{x:guid}
int
Matches a 32-bit integer value.
{x:int}
length
Matches a string with the specified length or within a specified range of lengths.
{x:length(6)} {x:length(1,20)}
long
Matches a 64-bit integer value.
{x:long}
max
Matches an integer with a maximum value.
{x:max(10)}
maxlength
Matches a string with a maximum length.
{x:maxlength(10)}
min
Matches an integer with a minimum value.
{x:min(10)}
minlength
Matches a string with a minimum length.
{x:minlength(10)}
range
Matches an integer within a range of values.
{x:range(10,50)}
regex
Matches a regular expression.
{x:regex(^d{3}-d{3}-d{4}$)}
Route Prefixes:
You can set a common prefix for an entire controller by using the [RoutePrefix] attribute. Use a tilde (~) on the method attribute to override the route prefix.