Top Three Features of ASP.NET MVC 5

ASP.NET MVC is Microsoft’s web application development technology based on the principle of the separation of concerns. MVC stands for Models, Views, and Controllers, respectively.

Prior to the MVC pattern, Web Forms was the prime Microsoft technology for web application development. However, Web Forms lacked the flexibility of development and loose coupling. MVC addressed these concerns.

This article presents a brief overview of the top three features of ASP.NET MVC 5, which is the 5th version of the Microsoft MVC technology.

1. Attribute Routing

The major difference in ASP.NET MVC and ASP.NET web forms is the way incoming requests are handled.

In Web Forms, incoming requests map to a file or resource on the webserver. For instance, the requested URL can be of the form of www.abcd.com/xyz.aspx. Here, the URL refers to an ASP.NET Web Form named xyz.aspx, located at the root directory. There is a one-to-one mapping between the URL and the page that is accessed, which must physically reside on the server.

On the other hand, MVC routing pattern maps URLs with action methods, resulting in cleaner and more SEO-friendly URLs (for instance, www.abcd.com/xyz/buy). This URL refers to the action method “buy” of the controller “xyz” on the domain “abcd”.

To see where these routes are configured, go to the solution explorer of your ASP.NET MVC application and find App_Start folder and find the RouteConfig.cs file. It contains a method named RegisterRoutes which takes a collection of type RouteCollection as a parameter. Inside this method are routes that are to be ignored; routes that are to be added can be defined. This is shown in the following code snippet:

public static void RegisterRoutes(RouteCollection routes){
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Option }
);
}

Apart from defining routes in the RouteConfig.cs file, ASP.NET MVC 5 has introduced a new feature known as attribute routing which allows developers to specify a route as an attribute of the action method. To enable attribute-based routing, you need to modify the RegisterRoutes method of RouteConfig.cs file as follows:

public static void RegisterRoutes(RouteCollection routes){
routes.MapMvcAttributeRoutes();
}

To add a route on any action method, you simple have to add an attribute named Route and pass the route as parameter to it. For instance:

[Route("Products/Sports/{id}")]
public ActionResult GetSportsItems(string id){
ViewBag.Id = id;
return View();
}

2. Default MVC Template Replaced by Bootstrap

ASP.NET MVC 5 replaced the default MVC template with a much more flexible and standardize CSS library called Bootstrap. With the integration of Bootstrap in MVC 5, developers have got a myriad of styling options right out of the box.

3. Improved Identity Management & Third-party Authentications

ASP.NET MVC 5 has introduced a more robust, secure, yet flexible identity management mechanism.

Now with MVC 5, developers need not explicitly manage identity and authentication of the application users. Rather, these ASP.NET MVC features come built-in with the framework and can be easily tweaked to achieve the desired identity and authentication operations.

Also, MVC 5 provides authentication and logic features via third-party applications such as Facebook, Google, and Twitter, all of them right out of the box.

Ashutosh Kumar

Ashutosh is a Senior Technical Architect at Taazaa. He has more than 15 years of experience in .Net Technology, and enjoys learning new technologies in order to provide fresh solutions for our clients.