Attribute based Routing is yet another new feature in MVC 5, in this feature you can apply route attribute on Controller and Action such that it influence selection of Controller and action Method.
Attribute based Routing is totally different from traditional routing in traditional routing we declare route in RouteConfig.cs file.
In Register Routes method we can declare as many as Route we want to use but must be according to proper hierarchy else it will not allow proper selection of route which will lead to errors [http not found].
Now let's dive in Attribute Routing.
The first step we are going to create a new project as shown below in step by step way.
Create New Asp.Net MVC Application
From Visual studio 2015 IDE Start page click on "New Project" link.
After clicking on "New Project" link a new dialog will pop up.
In that we are going to select web templates from left pane after selecting web template, we find only one project template in it "ASP.NET Web Application" just select that.
After selecting this project template next we are going to name the project as "MVC5DEMO6" and clicking on the OK button a new dialog will pop up with Name "New ASP.NET Project" for selecting project Templates.
In this dialog, we are going to choose MVC project template and then we are going to choose Authentication type for doing that just click on Change Authentication button, a new dialog will pop up with name "Change Authentication" here we are going to choose No Authentication .
After selecting Authentication type as No Authentication click on OK Button.
It will show a progress bar while it is creating a project.
After creating a project it will show Readme HTML page with use links on that page.
After creating project first thing we are going to do is Enabling Attribute Routing in RouteConfig.cs class.
Enabling Attribute Routing
For doing that first open RouteConfig file which is located in App_Start folder and then just adds routes.MapMvcAttributeRoutes(); method just above routes.MapRoute as the show is below snapshot.
Now we have enabled Attribute Routing for Entire application.
Next step we are going to add a new controller with name PropertyController.
Adding Property Controller
For adding controller just right on Controllers folder inside that select Add à inside that select Controller after selecting Controller a new dialog with name "Add Scaffold" will pop up for choosing type of controller to add in that we are just going to select "MVC5Controller - Empty" and click on Add button, after that a new dialog with name "Add Controller" will pop up asking for Controller name here we are going to name controller as PropertyController and click on Add button.
After adding Property controller next step we are going to rename Index action Method and add some new action method in this controller and along with their Views.
Adding New Action Method and their Views in Property Controller
In this part we have added 3 New Action Method Buy, Sell, and Rent in Property Controller.
After adding 3 new Action Method in Controller in next step we have added Views related to this Action Methods.
Now let's save this application and run.
Next we are going to access newly added Controller and Action Methods.
For Accessing Buy Action Method URL: http://localhost:####/Property/buy.
For Accessing Sell Action Method URL: http://localhost:####/Property/sell.
For Accessing Rent Action Method URL: http://localhost:####/Property/rent.
This all was working in traditional way now let's add Route.
Adding Route attribute
This attribute adds a route to Route table, and this attribute is applied to controller or action method.
Now let's apply this attribute to Action method first.
On Buy Action Method we are going to apply route as BuyProperty.
On Sell Action Method we are going to apply route as SellProperty.
On Rent Action Method we are going to apply route as RentProperty.
Now let's save this application and Run.
Now we are going to Access Buy action Method with this URL: http://localhost:####/BuyProperty
Now we are going to Access Sell action Method with this URL: http://localhost:####/SellProperty
After adding Route attribute next we are going to Add Route Prefixes attribute.
Adding Route Prefixes attribute
If we want to apply Common prefix [Property] (Static Word) for each Action Method in Controller then instead of writing in every route you can use [RoutePrefix("")] attribute and apply this attribute on Controller level.
Below is a scenario where attribute routing can be used.
After applying RoutePrefix attribute.
Below is snapshot after applying RoutePrefix attribute [[
Now after applying RoutePrefix attribute we need to append Property at the start of URL to access Action Method.
Access BuyProperty action Method with this URL: http://localhost:####/BuyProperty
Access SellProperty action Method with this URL: http://localhost:####/SellProperty
Access RentProperty action Method with this URL: http://localhost:####/Property/rent
After adding Route Prefixes attribute next we are going to Add Route Area attribute.
Adding Route Areas attribute
For showing a demo of Route Areas attribute first, we need to add Area.
For adding Area just right click on project "MVC5DEMO6" then select Add à from the list inside that select Area as shown below.
After choosing area a new dialog will pop up with Name "Add Area" in that just give the name of the area as per your requirement but we are going to name Area it as "Residential" and click on Add button.
The View of Project after adding Area "Residential".
After adding Area next thing we are going Controller in this Area with name "ResidentialProperty".
Inside that Controller, I have 2 action methods.
Now we are going to add RouteArea attribute "Residential" and added new route Prefix "NewResidentialProperty" and long with that we have added Route (Buy, Sell) as shown below.
Now if you run application along with changes we made it will not work properly because if we want to use Route attribute and along with Traditional based routing then we need to make change in sequence of AreaRegistration in Global.asax.
Default View of Global.asax
Changed View of Global.asax
The area registration must be done after Register Routes then it works properly.
Now save your application and Run.
RouteArea: - Residential, RoutePrefix: - NewResidentialProperty, Route: - BUY
After adding Route Area attribute next we are going to Add Route Constraints attribute.
Adding Route Constraints
A route constraint is a way to validate incoming URL parameters.
In below snapshot, you see have look how to validate incoming parameter.
List of Constraint
Finally we have learnt how to use Attribute Routing.