Introduction to ASP.NET MVC Framework
In this article, I am going to give you a brief Introduction to ASP.NET MVC Framework. As part of this article, we are going to discuss the following pointers in detail.
- What is ASP.NET MVC?
- What is MVC?
- How Does the MVC Design Pattern Work in ASP.NET MVC Application?
- Advantages of using ASP.NET MVC to Develop Web Applications
What is ASP.NET MVC?
The ASP.NET MVC is a web application development framework provided by Microsoft which is built on top of the .NET Framework. We can use this ASP.NET MVC Framework to develop web applications that provide a clean separation of code. The ASP.NET MVC framework is the most extensible and customizable framework provided by Microsoft.
The ASP.NET MVC Framework is based on MVC (Model-View-Controller) Design Pattern. So the point that I need to highlight here is ASP.NET MVC is a Framework whereas MVC is a Design Pattern.
The ASP.NET MVC Framework is not built from ground zero. You can consider it as an alternative approach to our traditional ASP.NET Web Forms Framework. As it is built on top of the .NET Framework, developers enjoy almost all the ASP.NET features while working with the MVC application.
What is MVC?
MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
MVC design pattern was introduced in the 1970s that basically divides an application into 3 major components as Model, View, and Controller. The main objective of the MVC design pattern is the separation of concerns (codes), which means the domain model and business logic are separated from the user interface (i.e. view). As a result, maintenance and testing of the application become simpler and easier.
How does the MVC Design Pattern work in ASP.NET MVC Application?
Let us understand how the MVC Design Pattern works in the ASP.NET MVC application with an example. Let’s say we want to display the student details on a web page as shown in the below image.
So, when the client (user) issues a request something like “http://dotnettutorials.net/student/details/2” from a web browser then the request is handled by the MVC framework as shown below.
The controller is the component in the ASP.NET MVC application that actually receives the incoming HTTP request and then handles that request. In order to handle the incoming HTTP request, the controller does several things as follows.
- The controller creates the model object that is required by a view. The model is the component in the MVC design pattern that contains a set of classes to represent the domain data or business data as well as logic to manage the data.
- The controller then selects a view to render the domain data or business data. The point that you need to remember is, while selecting a view, it is the responsibility of the controller to pass the model data.
- In the MVC Design Pattern, the one and only responsibility of a view is to display the model data. So, the responsibility of a view is to generate the necessary HTML which will render the model data or business data. Once the HTML is generated by the view, then that HTML is then sent to the client via the controller who initially made the request.
Now, I hope you understand the basic idea of the MVC Design Pattern. Let us discuss each of the components of the MVC design pattern in detail by comparing it with our example.
The Model is the component in the MVC design pattern that manages that business data or domain data i.e. state of the application in memory. The Model contains a set of classes that represent the data as well as logic to manage the data. So, in our example, the model consists of the Student class to represent the student data as well as StudentBusinessLayer class to retrieve the student data from any persistent medium like a database.
So in short, a Model:
- In ASP.NET MVC is basically a C# or VB.net class to represent the data as well as to manage the data.
- It is accessible by both controller and view.
- It can be used to pass data from controller action methods to a view.
- It can also be used by a view to display data on a page (HTML output).
The view is the component in MVC Design Pattern that renders the model data as the user interface with which the end-user can interact. So, the View creates the user interface with data from the model. In our example, we want to display the Student information on a web page. So here the student model carried the student data to the view. This is the student model which should be supplied by the controller to the view. The following code does the same thing.
@model FirstMVCApplication.Models.Student <html> <head> <title>Student Details</title> </head> <body> <br /> <br /> <table> <tr> <td>Student ID: </td> <td>@Model.StudentID</td> </tr> <tr> <td>Name: </td> <td>@Model.Name</td> </tr> <tr> <td>Gender: </td> <td>@Model.Gender </td> </tr> <tr> <td>Branch: </td> <td>@Model.Branch</td> </tr> <tr> <td>Section: </td> <td>@Model.Section </td> </tr> </table> </body> </html>
So in short, a View
- In ASP.NET MVC is a cshtml page.
- It contains all page-specific HTML generation and formatting code.
- A request to a view can only be made from a controller’s action method.
- The one and only responsibility of a view is to render the domain data or business data.
The Controller is the component that contains the control flow logic. It is the one that will interact with both models and views to control the flow of application execution. The controller is the component in MVC Design Pattern that will handle the incoming HTTP Request. Based on the user’s actions, the respective controller will work with the model and view and then sends the response back to the user who initially made the request. In our example, when the client issued a request to the following URL
Then that request is going to be mapped to the Details action method of the Student Controller. Following is the code of our Controller class with the Details action method.
So, in short, a Controller:
- Is basically a C# or VB.NET class that is inherited from the System.Web.Mvc.Controller.
- Is the component which will interact with both Models and views.
- Contains action methods that are responsible for handling the incoming HTTP Request.
- Can access and use the model class to pass the data to the views.
Advantages of using ASP.NET MVC Framework to Develop Web Applications
- It is lightweight because it does not use view state or server-based forms or server controls.
- Each developer based on his expertise or experience can work on different parts of the application. For example, one developer may work on the view while the second developer can work on the controller logic and the third developer may work on the business logic.
- It provides better support for test-driven development (TDD). This is because we can focus on one aspect at a time i.e. we can focus on the view without worrying about business logic.
- ASP.NET MVC Framework divides the application into three main aspects as Model, View, and Controller which make it easier to manage the application complexity.
- Another important advantage of the ASP.NET MVC framework is its components are designed to be extensible and pluggable and therefore they are easily replaced or customized.
- The MVC framework is built on top of the ASP.NET Framework and hence we can use most of the ASP.NET features such as authentication and authorization scenarios, membership and roles, caching, sessions, and many more.
- ASP.NET MVC framework supports a powerful URL routing mechanism (i.e. attribute routing) which helps to build more user-friendly and SEO-friendly URLs for our application.
In the next article, I am going to discuss the Step-By-Step Process to Create an ASP.NET MVC Application using Visual Studio. Here, in this article, I try to give you a brief introduction to ASP.NET MVC Framework. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.