Why ASP.net MVC?
Microsoft decided to create its own MVC framework for building web applications. The MVC framework builds on top of ASP.NET. When you build a web application with ASP.NET MVC, there will be no illusions of the state; there will not be such a thing as a page load and no page life cycle at all, etc.
Another design goal for MVC was to be extensible throughout all aspects of the framework. Therefore, when we talk about views, they have to be rendered by a particular type of view engine. The default view engine is something that can take an ASPX file. However, if you don’t like using ASPX files, you can use something else and plug in your view engine.
There is a component inside the framework that instantiates your controllers. Therefore, there are many places in MVC where you can inject your custom logic to handle tasks.
The whole concept behind using the Model View Controller design pattern is to maintain a separation of concerns. Your controller is no longer burdened with many ties to the ASP.NET runtime or ties to the ASPX page that is very hard to test. You now have a class with regular methods on it that you can invoke in unit tests to determine if that controller is going to behave correctly.
In a web application, there are mainly four aspects that define performance:
- Response time issues
- The problem of Unit Testing
- HTML customization
- Reusability of the code-behind class
ASP.net MVC excels on all the above parameters.
Version History of MVC:
- ASP.NET MVC 1.0
- Released on Mar 13, 2009.
- It runs on .NET 3.5.
- Visual Studio 2008.
- MVC Pattern architecture with WebForm Engine.
- Main Features include Html & Unit testing, Ajax Helpers, Routing, etc.
- ASP.NET MVC 2.0
- This version was released on March 10, 2010.
- It runs on .NET 3.5, 4.0 and with Microsoft Visual Studio 2008.
- Include Features such as Templated helpers, Ul helpers with automatic scaffolding & customizable templates.
- It supports DataAnnotations Attributes to apply model validation on client and server sides.
- ASP.NET MVC 3.0
- It was released on Jan 13, 2011.
- It runs on .NET 4.0 and with Microsoft Visual Studio 2010.
- It make use of NuGet to deliver software and allows you to manage dependencies across the platform.
- It offers features such as the Razor view engine; enhanced Data Annotations attributes for model validation on both client and server sides.
- ASP.NET MVC 4.0
- This version was released in Aug 2012.
- It runs on .NET 4.0, .NET 4.5 with Visual Studio 2010 & Visual Studio 2012.
- It enhances to default project templates.
- Offers features such as Mobile project template using jQuery Mobile, Task support for Asynchronous Controllers, bundling, minification, etc.
- ASP.NET MVC 5.0
- Released on 17 October 2013.
- It runs on .NET 4.5, 4.5.1 and with Visual Studio 2012, Visual Studio 2013 & Visual OneASP.NET.
- Supports attribute routing in MVC.
Features of MVC:
- Easy and frictionless testability.
- Leverage existing ASP.Net Features.
- A new presentation option for ASP.Net.
- A simpler way to program Asp.Net.
- Clear separation of logic: Model, View, Controller.
- Support for parallel development.
- It makes easier to manage complexity by dividing an application into the model, the view, and the controller.
- It also enables full control over the rendered HTML and provides a clean separation of concerns.
- It has direct control over HTML also means better accessibility for implementing compliance with evolving Web standards.
- It facilitates adding more interactivity and responsiveness to existing apps.
- It also gives better support for test-driven development (TDD).
- It also works well for Web applications supported by a large team of developers and Web designers who needs a high degree of control over the behavior of application.
MVC architectural Pattern:
MVC is a software architecture pattern that follows the separation of concerns method. In this model, .Net applications are mainly divided into three interconnected parts: Model, View, and Controller.
The MVC pattern’s goal is that each of these parts can be developed, tested in relative isolation, and combined to create a very robust application.
Let see all of them in detail:
Model objects are parts of the application that implement the logic for the application’s data domain. It retrieves and stores the model state in a database. For instance, a product object might retrieve information from a database, operate on it. Then write information back to the products table in the SQL server.
Views are the components used to display the application’s user interface (UI), also called the view model in MVC. It displays the .Net MVC applications that are created from the model data.
A common example is an edit view of an Item table that displays text boxes, pop-ups, and check-boxes based on the current state of products & objects.
Controllers handle user interaction, works with the model, and selects a view to render Ul. In a .Net MVC application, the view only displays the information; the controller manages and responds to the user input & interaction using action filters in MVC.
For instance, the controller manages query-string values and passes those values to the model.
MVC Life Cycle:
MVC has two life cycles:
- The application life cycle
- The request life cycle
The Application Life Cycle:
It denotes the time at which the application process starts running IIS until the time it stops and is marked by the application start and end events in the startup file of your application.
The Request Life Cycle:
It is the sequence of events that happens every time our application handles an HTTP request.
The entry point in every MVC application begins with the routing. After the .NET platform has received a request, it determines how it should be handled through the URL Routing Module.
Modules are the .NET components that can hook into the application life cycle and adds functionality to them. The routing module is responsible for matching incoming URL to the routes that we define in our application.
All the routes have an associated route handler with them, and this is the entry point to the MVC framework.
The MVC framework handles the conversion of the route data into a concrete controller that can handle requests. After the controller has been configured, the next step is Action Execution. A component named the action invoker finds and selects an appropriate Action method to invoke the controller.
After the action result has been prepared, the next stage triggers, which is the Result Execution. MVC separates, declares the result from executing the result. If the result is actually a view type, the View Engine will be called, and it is responsible for finding and rendering the view.
If the result is actually not a view, the action result will execute on its own. The Result Execution is what generates an actual response to the original HTTP request.
Web Forms vs. MVC:
Advantages of ASP.NET MVC:
- It is highly maintainable application by default.
- It also allows you to replace any component of the application.
- It provides better support for Test-Driven Development.
- Complex applications are easy to manage in MVC because of divisions of Model, View, and Controllers.
- It offers robust routing mechanism with front controller pattern.
- It offers better control over application behavior with the elimination of view state and server-based forms.
- These applications are supported by a large teams of developers and Web designers.
- It also offers more control over the behaviors of an application. It uses an optimized bandwidth for requests that are made to the server.
Disadvantages of ASP.NET MVC:
- You cannot see the design page preview such as the .aspx page.
- You also need to run the program every time to see its actual design.
- Understanding the flow of an application can be challenging.
- It is somehow complicated to implement, so it is not ideal for small level applications.
- It is also hard to learn MVC for ASP.NET, as it requires a great understanding of the MVC pattern.