In my last blog, I gave an explanation of how ASP.NET Web Forms work along with the detailed development of a sample application using it. Now that you know how to use this amazing technology, let’s explore another great framework that Microsoft provided us to build web applications. This framework, ASP.NET MVC, has become famous in a very short period of time compared to the ASP.NET Web Forms technology.
In this blog I will be presenting you with yet another ingenious .NET technology that works on a different pattern than ASP.NET Web Forms and provides us even better and faster tools in developing state of the art small-scale and large-scale web applications. That’s right! ASP.NET MVC, which started with a bang, has reached great heights in a very short time.
MVC stands for Model, View and Controller. The web applications built in this technology are mainly divided into these three architectural patterns as components, i.e. the model, the view and the controller. It is a very flexible and lightweight framework that gives you great power when developing web applications.
I was very excited to learn this new way of developing web applications when it was launched. In this blog, I will be giving you a brief intro of the ASP.NET MVC framework along with some detail as to how it works and then we will move on to developing a good old web application using this exciting technology. So here it is my fellow nerds: ASP.NET MVC!
As I told you before, ASP.NET is an ever growing technology for Web application development and ASP.NET MVC is yet another great tool added to its arsenal in 2009. This tool uses the more common three-tier architectural pattern that most of us use in our day-to-day project development. Generally if we are building a web application using this three-tier architecture, we know them as the data tier, the business logic tier and the presentation tier. ASP.NET MVC gives us the power to use a similar three-tier approach, but it calls it the Model View Controller pattern. This MVC division is really helpful when it comes to developing complex web applications because it provides you the ability to work and design on one aspect of the application at a time. In addition to ease of testing the application, it also helps in dividing the tasks amongst a group of developers to easily manage the application development.
According to the official ASP.NET MVC website:
“ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable, agile development. ASP.NET MVC includes many features that enable fast, TDD-friendly development for creating sophisticated applications that use the latest web standards.”
I think that is about the simplest and yet fully detailed explanation of what ASP.NET MVC really is and what it helps us achieve. MVC uses three major components, i.e. the model, the view and the controller. By separating your web application into these three pieces, it gives you the power to manipulate them independently and according to need. No more mixing of the UI and the Logic at one place and no need to plan and design architecture manually to handle these separately. Let us now see what these components are and how they work with each other.
Oh! Before we move on to how this amazing technology works, let me show you the history of releases this framework has had over the years and also what is the latest version that is currently in use.
10 December 2007 – ASP.NET MVC CTP
13 March 2009 – ASP.NET MVC 1.0
16 December 2009 – ASP.NET MVC 2 RC
4 February 2010 – ASP.NET MVC 2 RC 2
10 March 2010 – ASP.NET MVC 2
6 October 2010 – ASP.NET MVC 3 Beta
9 November 2010 – ASP.NET MVC 3 RC
10 December 2010 – ASP.NET MVC 3 RC 2
13 January 2011 – ASP.NET MVC 3
20 September 2011 – ASP.NET MVC 4 Developer Preview
15 February 2012 – ASP.NET MVC 4 Beta
31 May 2012 – ASP.NET MVC 4 RC
15 August 2012 – ASP.NET MVC 4
30 May 2013 – ASP.NET MVC 4 4.0.30506.0
26 June 2013 – ASP.NET MVC 5 Preview
23 August 2013 – ASP.NET MVC 5 RC 1
17 October 2013 – ASP.NET MVC 5
17 January 2014 – ASP.NET MVC 5.1
As mentioned before, ASP.NET MVC is composed of 3 components as follows:
1. The model.
2. The view.
3. The controller.
The model is responsible for core data handling, i.e. interacting with the database records like saving, retrieving, updating and deleting from the source database. Model actually implements the logic of an application, which interacts with the data domain. It also stores the state of the object in the database. For example, a “Vehicle” model object can retrieve data from the source, manipulate it and then update the changed or added information back to the source database.
The view is responsible for showing the data to the user, or in short terms, making the UI of your application that the user sees. The view develops itself mostly from the model data, i.e. it links itself to the model which provides the data you need to show to the user. For example, an edit view of the vehicle model/table would display textboxes, dropdown lists, etc., based on what the vehicle object state is.
The controller is responsible for handling user interactions, communicating with the model and, based on input/interaction, selecting the appropriate view to display. For example, the controller uses the query-string values or user input and passes them to the model which will use them to do the necessary database interaction, depending on the values or input.
All of these components are loosely coupled, but they also tend to be in separate sections within the application. This separation/pattern in ASP.NET MVC has many benefits and features that include:
• Giving you the ability to separately manage input logic, business logic and UI logic.
• Giving you the ability to do parallel development, i.e. one developer can work on a view while the other can work on the controller business logic.
• Giving you better support for fast test driven development, due to the isolation of these components from the rest of the framework.
• It does not have any view state or server-based forms to manage. This makes the site work a lot faster and also gives you full control of the behaviour of the application, down to the smallest part of it.
• It processes web application requests by using a single controller (often known as the front controller). This gives you the ability to design your application supporting rich routing infrastructure.
• Extensive ASP.NET routing support, enabling you to build applications that have searchable URLs.
• Ability to use existing ASP.NET features like master pages, user controls, inline expressions, templates, data-binding and declarative server controls.
• Support for existing security features like forms authentication and windows authentication, URL authorization, memberships and roles.
The MVC request execution has different stages which it goes through. These are as follows courtesy of MSDN.
Another new thing that we see with MVC is something called Razor Syntax. As in the ASP.NET Web Forms technology, we use <% %> blocks to add server side code into the html markup of the page. Razor syntax is similar to that except it starts with an @ instead of the blocks above and also you do not need to close this explicitly. Razor Syntax runs on the server in a View Engine that processes it and converts the code into html markup before sending it out to the browser. This gives your view the ability to be extremely lightweight as well as customizable to the tiniest extent. This new view engine came with the MVC 3 version and the files that support this syntax have the extension .cshtml for C# and .vbhtml for VB.NET. To get to know more on this syntax, please go over the following blog and all its chapters before moving on with this one. It will really help you understand the advantages of this new view engine as well as help you understand my demo of MVC more clearly.
Lastly, I want to mention that you can also integrate an ASP.NET MVC web application with Web Forms and vice versa, since the underlying framework is still ASP.NET. You can combine most ASP.NET Web technologies in a single web application. As of right now, Visual Studio 2013 gives you the power to combine these ASP.NET Web Technologies into a single project while creating it. Before this you needed to create a web application by selecting the technology you wanted to use, i.e. ASP.NET Web Forms or MVC, and then merge the other technologies into it manually.
To learn how to use ASP.NET MVC practically, please visit the second part of this blog at the following link: