Routing in Asp.NET MVC is how ASP.NET MVC matches a URI to an action. ASP.NET routing enables you to
use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a
file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.
You can find more details about ASP.NET MVC Routing in separate articles. Here, I am going to discuss about
Attributes in Routing in ASP.NET MVC.Now, you are able to know what following code says.
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Attributes in Routing in ASP.NET MVC with example:
1. 7/16/2014 Attributes in Routing in ASP.NET MVC with example:
http://www.dotnet-stuff.com/tutorials/aspnet-mvc/attributes-in-routing-in-asp-net-mvc-with-example 1/6
R
Attributes in Routing in ASP.NET MVC with example:
Posted Date: 26. May 2014 Posted By: Anil Sharma
Categories: ASP.NET MVC, ASP.NET
Keywords: Routing in ASP.NET MVC, Attributes in Routing, Routing Attributes, MVC Routing Attributes, Routing
Attributes Example
outing in Asp.NET MVC is how ASP.NET MVC matches a URI to an action. ASP.NET routing enables you to
use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a
file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.
You can find more details about ASP.NET MVC Routing in separate articles. Here, I am going to discuss about
Attributes in Routing in ASP.NET MVC.Now, you are able to know what following code says.
Here, we have defined a route for the application. When the route definitions are co-located with the actions, within
the same source file rather than being declared on an external configuration class, it can make it easier to reason
about the mapping between URIs and actions. The previous route definition would be set using the following, simple
attribute:
How to Enable Attribute Routing
1
2
3
4
5
6
7
8
9
10
11
12
13
publicstaticvoidRegisterRoutes(RouteCollectionroutes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categories.aspx",
true,
newRouteValueDictionary
{{"categoryName","MVC"},{"action","show"}});
}
voidApplication_Start(objectsender,EventArgse)
{
RegisterRoutes(RouteTable.Routes);
}
1
2
[Route("{categoryId:int}/{categoryTitle}")]
publicActionResultShow(intcategoryId){...}
2. 7/16/2014 Attributes in Routing in ASP.NET MVC with example:
http://www.dotnet-stuff.com/tutorials/aspnet-mvc/attributes-in-routing-in-asp-net-mvc-with-example 2/6
It is very easy to enable Attribute in Routing,You just need to call MapMvcAttributeRoutes during configuration.
Following is an example to enable attribute in Routing.
Attribute Routing in ASP.NET MVC Example
After Enabling the attributes in application you can define a route attribute on top of an action method. The following
is the example of a Route Attribute in which routing is defined where the action method is defined. In the following
example, I am defining the “Route” attribute on top of the action method.
Attribute Routing with Optional Parameter
It is possible that we may have the requirement for something that must accept some options parameter. We can
define an optional parameter in the URL pattern by defining a question mark (“?") to the route parameter. We can
also define the default value by using parameter=value.
1
2
3
4
5
6
7
8
9
10
publicstaticvoidRegisterRoutes(RouteCollectionroutes)
{
routes.MapMvcAttributeRoutes();
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categories.aspx",
true,
newRouteValueDictionary
{{"categoryName","MVC"},{"action","show"}});
}
1
2
3
4
5
6
7
8
9
publicclassHomeController:Controller
{
//URL:/Category
[Route("Category")]
publicActionResultIndex(){
ViewBag.Message="WelcometoASP.NETMVCRouting!";
returnView();
}
}
1
2
3
4
publicclassHomeController:Controller
{
//OptionalURIParameter
3. 7/16/2014 Attributes in Routing in ASP.NET MVC with example:
http://www.dotnet-stuff.com/tutorials/aspnet-mvc/attributes-in-routing-in-asp-net-mvc-with-example 3/6
Route Prefixes in Routing in ASP.NET MVC
Sometimes, we may got the requirements which include some prefix in the entire Action Methods. We can also set a
common prefix for the entire controller (all action methods within the controller) using the “RoutePrefix” attribute.
Following is an example shows how we can define Route Prefix.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//URL:/Category/
//URL:/Category/MVC
[Route("Category/{categoryName?}")]
publicActionResultShow(stringcategoryName){
ViewBag.Message="ThisisAttributeRoutingwithOptionalParameterASP.NETMVC!";
returnView();
}
//OptionalURIParameterwithdefaultvalue
//URL:/Category/
//URL:/Category/MVC
[Route("Category/{categoryName="MVC"}")]
publicActionResultShow(stringcategoryName){
ViewBag.Message="ThisisAttributeRoutingwithDefaultValueASP.NETMVC!";
returnView();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[RoutePrefix("Category")]
publicclassHomeController:Controller
{
//URL:/Category/
[Route]
publicActionResultIndex()
{
ViewBag.Message="WelcometoDotNetStuff!";
returnView();
}
//OptionalURIParameter
//URL:/Category/
//URL:/Category/MVC
[Route("{categoryName}")]
publicActionResultShow(stringcategoryName)
{
ViewBag.Message="WelcometoASP.NETMVCCategory!";
returnView();
}
}
4. 7/16/2014 Attributes in Routing in ASP.NET MVC with example:
http://www.dotnet-stuff.com/tutorials/aspnet-mvc/attributes-in-routing-in-asp-net-mvc-with-example 4/6
If we want to override the route prefix, we should use a tide (~) sign with the Route attribute. Following is an example
which shows how it can be done.
Defining Default Route using Route Attribute in ASP.NET
MVC Routing
If we want to define default route for any controller, We can do it by defining a Route attribute on top of the
controller, to capture the default action method as the parameter. Following example shows how it can be done.
20
1
2
3
4
5
6
7
8
9
10
11
[RoutePrefix("Category")]
publicclassHomeController:Controller
{
//URL:/OverideRoutePrefix/
[Route("~/NewCategory")]
publicActionResultIndex()
{
ViewBag.Message="Overridetherouteprefix!";
returnView();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[RoutePrefix("Category")]
[Route("action=index")]
publicclassHomeController:Controller
{
//URL:/Category/
publicActionResultIndex()
{
ViewBag.Message="WelcometoASP.NETMVCRouting!";
returnView();
}
//URL:/Category/NewCategory
publicActionResultNewIndex()
{
ViewBag.Message="ThisisnewCategoryforASP.NETMVC!";
returnView();
}
}
5. 7/16/2014 Attributes in Routing in ASP.NET MVC with example:
http://www.dotnet-stuff.com/tutorials/aspnet-mvc/attributes-in-routing-in-asp-net-mvc-with-example 5/6
Defining Route name in ASP.NET MVC
To define route name in ASP.NET MVC Routing. We can also define a name of the route to allow easy URI
generation. Following example show how to do that.
And to generate URI from above definition of Routing. We can generate URI using Url.RouteUrl method.
Defining Area in ASP.NET MVC Routing
Areas are logical grouping of Controller, Models and Views and other related folders for a module in MVC
applications. By convention, a top Areas folder can contain multiple areas. Using areas, we can write more
maintainable code for an application cleanly separated according to the modules.
If we have Areas in our application, than we can define the "Area" name from the controller that belongs to the using
RouteArea attribute. If we define the “RouteArea” attribute on top of the controller, we can remove the
AreaRegistration class from global.asax. Following is an example to shows how to defining Area in Asp.NET MVC
Routing.
1
2
3
4
5
6
[Route("Category", Name="CategoryURL")]
publicActionResultIndex()
{
ViewBag.Message="WelcometoASP.NETMVCRouting!";
returnView();
}
1 <ahref="@Url.RouteUrl("category")"="">CategoryURI</a>
1
2
3
4
5
6
7
8
9
10
11
12
[RouteArea("PostArea")]
[RoutePrefix("Category")]
[Route("action=index")]
publicclassHomeController:Controller
{
//URL:/PostArea/Category/
publicActionResultIndex()
{
ViewBag.Message="WelcometoASP.NETMVCRouting!";
returnView();
}
}
6. 7/16/2014 Attributes in Routing in ASP.NET MVC with example:
http://www.dotnet-stuff.com/tutorials/aspnet-mvc/attributes-in-routing-in-asp-net-mvc-with-example 6/6
Summary:We can define Routing in a separate file but using Attribute Routing allows us more control over the URIs
in our MVC web application. The earlier way of routing (convention-based routing) is fully supported by this version
of MVC. We can also use both type of routing in the same project. To know convention-based routing you can find it
in a separate article.
Keen to here from you...!
If you have any questions related to what's mentioned in the article or need help with any issue, ask it, I would love to
here from you. Please MakeUseOf Contact and i will be more than happy to help.
About the author
Anil Sharma is Chief Editor of dotnet-stuff.com. He's a software professional
and loves to work with Microsoft .Net. He's usually writes articles about .Net
related technologies and here to shares his experiences, personal notes,
Tutorials, Examples, Problems & Solutions, Code Snippets, Reference
Manual and Resources with C#, Asp.Net, Linq , Ajax, MVC, Entity
Framework, WCF, SQL Server, jQuery, Visual Studio and much more...!!!
Connect with the author: • Google + • Linkedin