Difference Between ASP.NET MVC And ASP.NET

In this article we will try to understand the difference between Asp.Net and Asp.Net MVC. These differences are conceptual

Below are the points which 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 details.

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 execution of one specific activity/rask, 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.

  1. ActionResult index() {  
  2.     If(user == ”admin) {  
  3.         Return(“AdminView);  
  4.     }  
  5.     Else {  
  6.         Return View(“NormalView”);  
  7.     }  

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 result.

  1. ActionResult index(string device) {  
  2.         If(device == ”Desktop”) {  
  3.             Return(“DesktopView”);  
  4.         }  
  5.         Else If(device == ”Mobile”) {  
  6.             Return View(“MobileView”);  
  7.         }  
  8.         Else {  
  9.             Return JsonResult();  
  10.         } 
Flexible combination of View and Data

When we send a request, we get a response:

  • View,
  • View + Data

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.