Below are the points that we will be discussing
- View-Based Architecture
- Behind Code is not reusable
- Fixed Response type
- Flexible combination of View and Data
- Unit Testing
Now, let’s discuss these points in detail.
View-Based Architecture
View-based architecture is delivered by action-based programming.
Now, in ASP.NET, when the request completes, the page life cycle gets executed. If you have written any code on this, then it will execute. And, after that, the button click code will execute, which is really very complicated because, for the execution of one specific activity/task, all other activities are getting executed.
So, whenever the user sends a request, it goes to the View and the View keeps executing the life cycle.
Now, in this case, to reach the necessary action, you have to go through the page life cycle. So, the logical answer is to hit the action directly. That is what happens in MVC when requests are sent and the specific action gets executed. Also, all the activity (the code written in that action) gets executed and the View gets invoked.
This is a correct logical approach. Here, we are selecting the action and executing it. After that, we are calling an appropriate View.
So for an Action-based requirement, we are selecting View-based architecture.
Behind Code is not reusable
In ASP.NET, if you want to display the View on the basis of a condition; such as - displaying Admin view to the admins and normal View to the normal users, you have to write code in the page_load event with Response.
And again, the life cycle to that specific page will get executed.
Now in ASP.NET MVC, you can do it in the action itself.
ActionResult Index()
{
if (user == "admin")
{
return View("AdminView");
}
else
{
return View("NormalView");
}
}
So, it says we cannot use code behind code in another View. But in Controller, we can reuse the code.
Fixed Response type
HTML is not the only response type. When it comes to a web application, most of the people think that it needs to be HTML but that is not always true. There are cases when the output needs to be sent in XML format or if you are working with JavaScript, then you need JSON.
Suppose, the request is coming from the desktop, then it must return the desktop View. Likewise, for mobile devices, it should be Mobile View and for JavaScript, it must return the result in JSON.
So, we just have to add one more "if" statement for JSON results.
ActionResult Index(string device)
{
if (device == "Desktop")
{
return View("DesktopView");
}
else if (device == "Mobile")
{
return View("MobileView");
}
else
{
return JsonResult();
}
}
Flexible combination of View and Data
When we send a request, we get a response.
In ASP.NET, the framework is based on view first architecture, in which the view has been already decided. However, in the case of MVC, since the action first gets executed, it can go and pick any type of View, i.e., either only View or View+ data.
For this to happen in ASP.NET, we need to write a lot of code for the same.
Unit Testing
MVC Supports TDD, so you can perform testing in a better way as compared to ASP.NET.
That's it. Stay connected for more articles.