Back to: ASP.NET MVC Tutorial For Beginners and Professionals
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.
Model:
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).
View:
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.
Controller:
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
http://dotnettutorials.net/student/details/2
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.
- Clean HTML and easy integration with javascript and jQuery.
- 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.
Awsome article, great thanks to you
Great article. Simple to understand. Thank you.
for a beginner, so easy to understand. Thanks
I was finding a good one course about MVC but i didn’t found…I found the best course. I’ll recomend it to my friends! Thank you.
This article is very easy to understand and implement for beginners………!
It is a best plat form to learn from the scratch.
Really hard information !!!!
Please someone clarify that below sentence is correct?
The ASP.NET MVC is a web application development framework provided by Microsoft which is built on top of the .NET Framework.
My opinion:
The ASP.NET is a web application development framework provided by Microsoft.
MVC is Architecture.
MVC is an architectural pattern which can be implemented in any type application, i.e – Web Application, windows or other types(MVC concept can be implemented in Java and other platforms), that means, it is a pattern or concept for separation of concern which provides other advantages.
Framework: it is nothing but a set of libraries under one umbrella which caters many needs during the stages of request and response lifecycle
.NetFramework: Huge library base for all types application
Primarily gives CLR Engine
Garbage Collection, Exception Handling, Language Interoparability (cross lang between dlls – IL), Security
Asp.Net Framework: System.Web
Request. Response, HttpContext, HttpHandlers and Modules, Authentication, Authorization, Caching, Network Interaction with Files or Database – All these are different libraries available to support Web framework i.e. Asp.Net
ASP.Net.MVC:
Reason: ASP.Net is cumbersome because View and Model has no separation which consumes heavy network bandwidth as well as difficult to code and maintain
Now comes the MVC(design pattern) implementation on top of ASP.Net, which is ASP.Net.MVC and it is lightweight because of Separation of concerns. Responsibilities got shared at different levent. Faster way to code , easy way to maintain because you can write code in smallest chunks, these smallest chunks can be easily tested because Responsibilties were shared and Dependencies were separated well.
I tried giving explanation from my end with my context, anyone can correct it or you can mention if it is not addressing the point. I just added my cent for a better understanding.
Jazab ka aritcale simpale language and easy understanding.
Great
Can we follow this tutorial to learn .NET5?
All Courses are well explained in easier manner.
Thanks a Lot!!!
Is there any PDF version available for this course?
excelent
awesome
best course ever for beginners
Thank You!
Thanks a lot
very easy to understand for the beginners, Thanks a Lot!!!!!
Great
Yes
Very nice
Great!
very nice article
I have some doubts on it
When it comes to the JavaScript environment we have library and framework react and angular for the frontend and express and nest for the backend.
In case of the C# environment we have ASP which supports for the entire application or we can use for the backed purpose.
when we have to use webform and Mvc and Webapi ?
If we have choosen the Webapi and can we implement the Mvc ?
If we create the MVC then what frontend person do and what backend persons do because in Javscript we have react for frontend and express for backend,they create one project and work on the respective parts?
If we send the html from backedn then what frontend will do?