What is ASP.NET MVC? and MVC Architecture

A free video tutorial from Web University by Harsha Vardhan
MS Certified Trainer | 70000+ Happy Students
13 courses
90,938 students
Learn more from the full course
Asp.Net MVC 5 - Ultimate Guide - In depth & Sample Project
Asp.Net Mvc, Asp.Net Core Mvc, Repository Pattern, Web Api, Asp.Net Identity, Entity Framework
21:40:28 of on-demand video • Updated March 2023
Confidently speak about MVC Architecture
Crack Asp .Net Mvc job interviews easier
Solve most common bugs and issues in Asp .Net Mvc applications
Working with Razor view engine, code like Pro
Secure Angular applications using the most-recommended - Asp .Net Identity
Interact with server using AJAX and build RESTful Services using Web API
Create real-world apps using Asp .Net Mvc 5
Create Asp .Net Mvc App Folder Structure with industry best practices like a Pro
English
Lets understand what is Asp.Net MVC and its benefits. Also in this lesson I am going to explain what is Model, View and Controller; how they interact. Asp.Net MVC is a "Web Application Framework", that gives you a powerful pattern based on the way to build dynamic web applications, that enables clean separation of concerns and that gives you full control of over markup. This definition seems really cool;
But what does it mean exactly? Typically a server side program receives a request from the client, performs some business logic and interacts with the database and finally sends the response back to the browser. Generally you can't create a web application straight-away based on a single programming language. Always you require a "Web Application Framework" to create web applications. Asp.Net is a "web application platform or framework" to create dynamic web applications that interacts with the server continuously, exchanging lots of information between client and sever. In Asp.Net, traditional web applications were built by using "Asp.Net WebForms Framework", which is a part of Asp.net. In Asp.net WebForms, the developers use "server side controls", post-backs, page life cycle etc. The main problems of Asp.Net Web Forms are two. First one is "no clean separation of concerns"; means you cannot separate the ASPX file and ASPX.CS file. You can't develop them or test them independently without having each other. Second one is: because of server side controls, post backs and page life cycle, lots of burden on the server to process many controls for each request. So finally you will get a slower performance. To overcome these two problems, Asp.Net MVC framework was introduced by Microsoft in 2009. So how the ASP.NET MVC framework solves above problems? Asp.net MVC allows you to build web applications based on MVC architecture. That means you are dividing the application code into three major parts called Model, View and Controller. The biggest advantage of MVC is, "Clean Separation of Concerns". That means you can develop and test the model, view and controller independently without having each other. So different teams of your organization can develop them independently. The second problem of Asp.net Web Forms is, slower performance due to server controls and page life cycle. In ASP.net MVC, server controls are eliminated. The design of MVC doesn't require any server controls. So the related concepts such as page life cycle, view state etc., are not required. Hence removed. The ASP.net MVC provides you the clean and clear way of developing dynamic web applications based on real stateless nature of the HTTP protocol. The ASP.NET MVC is a part of Asp.Net.
It is not isolated from ASP.net. So ASP.net has two major parts: ASP.net Web Forms and ASP.NET MVC. Of course, we also have ASP.net Web API; but we will discuss about it later in this course. ASP.NET MVC is an alternative to Asp.Net Web Forms. It is not built on the top of ASP.net Web Forms. It was introduced in 2009; And the current version of MVC is 5.2.5. What is MVC? MVC is an architectural pattern that dictates you to write the application code as composition of three major parts. First one is model. Model is a class that contains the "data structure" and "business logic". For example, you are developing an e-commerce application; and in a page, you want to display list of products to the user. So what fields you want to display? For example product id, product name, price, discounts etc. You have to create a C# class with these properties; and this class is called as Data Structure Model. This is one type of model. You have to create another C# class that contains business logic, which includes programming code that checks the business rules, calling data access layer to get the data from database, validating it, making calculations etc. This class is called as Business Logic Model; It is a second type of model class. Second one is view. It contains presentation logic to display the model data. So view accesses the model properties; i.e. in this case, the "data structure model". That means view reads the model properties from the data structure model and render those values on to the HTML result. View contains HTML, CSS, JavaScript and C#.net code to render the output. The third one is Controller. Controller defines execution flow. That means in MVC architecture, View or Model cannot execute upon receiving a request. When the request is sent, first the controller executes; Then controller calls the model; that means controller creates a model object; then the controller calls the view; and controller passes the model object to the view. Then the view reads necessary properties from the model object. Here, the most important point is, while the view is reading the properties from the model, the controller doesn't involve. For example, the Product View reads the properties of Product Model such as ProductID, ProductName, Price etc. But while the view is reading the model properties, the controller doesn't involve. Thus we can say a "view reads model". So the three important points of MVC is:
1. Controller calls Model.
2. Controller calls View and the passes Model object to view.
3. View calls model. What are the benefits of ASP.NET MVC? It supports "Clean Separation of Concerns". That means we can decompose large application into small units such as Data layer, Service layer, Presentation layer, Controller layer and allow them to be developed independently and parallelly. For example, one developer is developing the code for data access; at the same time you are developing the controller; and at the same time, another developer is developing the view;
and all these happen parallelly. So you can accelerate the application development and make it faster. Supports Unit Testing. Unit testing is a concept of testing a smallest piece of the code independently whether is it returning expected result or not. You can test the controllers and the business layers effectively in ASP.NET MVC. Supports Dependency Injection. Dependency injection is a concept of loading business objects into controller dynamically, rather than at compilation time. Your controller has a reference variable that holds the reference of the business logic object. You may have multiple business logic objects; one is dealing with the file storage and other one is dealing with the database storage. But the controller don't know which business logic object will be loaded into the reference variable. You will configure it in the configuration file; So the framework creates a business object and injects the same business object into the reference available that is present at the controller. So loading the business object into the reference variable, that is present at the controller at runtime, is called as Dependency Injection. Supports faster performance than ASP.net Web Forms. ASP.net Web Forms struggles with the slow performance, because of heavy page life cycle, View State and Post Backs. That means for every small change, the entire page needs to be refreshed. But in MVC, we never refresh the page; that means no PostBack in MVC, no View State, no Server side controls; so you will get the best performance of your server. Lets revise what we have understood in this lesson. ASP.net MVC is a web application framework, built based on MVC architecture, which is a part of ASP.NET; and supports Clean separation of concerns and full control over markup. The model contains the data structure and business logic;
View contains presentation logic; Controller defines execution flow;
Controller passes essential data to the view; that means controller passes model object to view. This is all about MVC architecture. Thank you and we will meet in the next lesson.