Features of ASP.NET MVC
ASP.NET MVC is an opinionated application development framework that prefers some functionality to be handled in a certain unique way. Let us discuss each of the features of ASP.NET MVC, along with the benefits they bring to the table.
Convention over configuration
This is a design methodology that significantly reduces the number of decisions while developing the application, and thus making it simpler.
If you have built any application using any technology, you might be using some kind of XML file where you have to configure everything in it. Even for the simpler straightforward things, we might have to configure the things over there.
ASP.NET MVC embraces convention over configuration completely. It is the philosophy where you can be certain of how it is going to work without ever configuring same.
Let me give you a simple example. All Controller code resides in the Controller
folder, and Views have a separate folder for each of the Controllers. Whenever a request comes, ASP.NET MVC knows where to find the Controller and its associated View without any configuration. This methodology results in less configuration and less time in debugging.
Separation of concerns
As discussed earlier, ASP.NET MVC has three major components—Model, Controller, and Views. This clearly separates the responsibilities so that the UI designer or UI developer can work on the View while backend developers can work on the Model to build a data domain for the application or to talk to the database. As the duties of each of the components are clearly defined and separated, the work can be done in parallel.
Control over the generated HTML
If you have any experience in building an ASP.NET Web Forms application, you might have used ASP controls such as asp:textbox
. Even though these controls have a lot of benefits, they have their cons as well. Developers cannot have complete control over the generated HTML when using these controls. Of course, you can set some properties in ASP control which in turn set some attributes in your generated HTML. But complete control is not possible. ASP.NET MVC HTML helpers and Tag helpers in ASP.NET Core provide better control over the generated HTML.
Better support for unit testing
As each of the components is separated and compartmentalized, creating the unit test cases becomes easier to achieve:
- Unified MVC and Web API Controller in ASP.NET Core: In earlier versions of ASP.NET MVC, different controllers were used for MVC (
System.Web.MVC.Controller
) and Web API (System.Web.Http.ApiController
). In ASP.NET Core, there is only one base controller that supports creating both MVC controllers and Web API controllers. With respect to routing, all the controllers use the same routes. Of course, you can use convention-based routing or attribute-based routing depending on your needs. - Note about Web API: Web API is the Microsoft technology for building web services over the HTTP protocol. HTTP is not only limited to serving web pages. Web API could be used for building API services and data. The advantage of this approach is that the services which are built using Web API could be consumed by a wide range of clients such as, browsers, mobile applications, and desktop applications.
The code for the earlier version of ASP.NET MVC (till ASP.NET MVC 5) is as follows:
publicclassValuesController : ApiController { // GET api/values publicIEnumerable<string>Get() { returnnewstring[] { "value1","value2"}; } } Code for ASP.NET Core: publicclassValuesController:Controller { //GET api/values [HttpGet] publicIEnumerable<string>Get() { returnnewstring[] { "value1","value2"}; } }