Attribute Routing in ASP.NET MVC 5

ASP.NET MVC 5 introduced Attribute Routing. It allows you to apply a route directly on the controller class. Before we use the Attribute Routing, let’s relook how the convention based routing works.

We create a route as follows:


Index action of Home controller can be accessed on two different URLs. Either using the base URL (default value is set) or using baseurl/Home/Index as shown below:


In convention based routing action is accessed using the baseurl/controllername/actionname. Using the Attribute Routing this behaviour can be overridden. To use attribute routing you need to enable it in RouteConfig. To enable it, in the RoutConfig.cs call routes.MapMvcRouteAttribute() method.


Once it is enabled you can apply attribute routing on action using Route attribute and passing a value in this.


Now Index action can be access on the URL: baseurl/Test


You can also pass parameters in the attribute route. Parameters name and variables in route value must be the same.


Index action can be access with parameter values as shown below- I am passing 40 as value of age and steve as value of name.


You may want to use the attribute routing in your project as per the requirement. Keep in mind that attribute routing and the convention based routing can be used together.

Happy coding.

Implement Step-by-Step Generic Repository Pattern in C#

Read full article on Falafel blog

It is not a good idea to access the database logic directly in the business logic. Tight coupling of the database logic in the business logic make applications tough to test and extend further. Direct access of the data in the business logic may cause problems such as

  1. Difficulty completing Unit Test of the business logic
  2. Business logic cannot be tested without the dependencies of external systems like database
  3. Duplicate data access code throughout the business layer

Without a Repository, an application will be seen as below:


Repository Pattern separates the data access logic and maps it to the entities in the business logic. It works with the domain entities and performs data access logic. In the Repository pattern, the domain entities, the data access logic and the business logic talk to each other using interfaces. It hides the details of data access from the business logic. In other words, business logic can access the data object without having knowledge of the underlying data access architecture. For example, in the Repository pattern, business logic is not aware whether the application is using LINQ to SQL or ADO.NET Entity Model ORM. In the future, underlying data sources or architecture can be changed without affecting the business logic.

Read full article on Falafel blog