I would like to write down my personal first experiences with ASP.NET MVC here and for all developers out there who are going to have such a first look into MVC with ASP.NET too, I’m writing that in English.
This article (and the series around it built step by step) is not intended to be a professional introduction to ASP.NET MVC. There are several websites giving very nice tutorials. But when I startet developing my first ASP.NET MVC website, there were some things most often in detail I had problems with and this is exactly the point. I’ll give a personal view on the experiences with ASP.NET MVC and maybe this helps one or another developer later.
In this first part I would like to state some notes on the MVC design pattern and the main difference(s) compared to „classic“ ASP.NET web applications.
ASP.NET Web Applications
I’m developing web applications for several years now and I was using the ASP.NET Web Application template most often. This template is a very nice approach for software developers who are familiar with Windows Forms development, because the main aspect of ASP.NET Web Applications is the fact, that it tries to circumvent the state-less HTT protocol. That means, normally a communication with a web server is the following way: The client requests a specific page, the server performs the request handling, renders the answer and is sending that answer back to the client. As soon as that happened, the server „forgets“ about that communication, which is meant by „state-less“. A follow-up request from the same client acts like a completely new request and communication between client and server.
ASP.NET web applications are using the so-called „Session State“ to save information about prior communication between both actors. In most cases this session state is encrypted and written to each HTML response, the server creates and sends back to the client in a hidden form field. If a client is requesting a page, all necessary information about the request e.g. form field input values are written to the hidden field and sent to the client. The client is performing a „Postback“ to the server including the session state, so the server is able to retrieve all necessary information to act like the request is „state-full“ instead of normal HTTP state-less communication.
The use of session states enable a model of programming a web application which is very unfamiliar for e.g. PHP developers. ASP.NET web applications enable the developer to act like a Windows Form which means the most important aspect: Event handling. Normally you would create a form tag in a web page, insert a button and when performing request handling on the server-side react on the forms submit state. That would mean, you have to know exactly, which control raised the request. This is done by ASP.NET framework for you in ASP.NET web applications. There is only one form tage permitted on a page and you define controls on the page. Afterwards you are writing control event handlers exactly for the controls and events you would like to be handled. Implementing AJAX is very easy using that event-driven point of view.
The abbreviation MVC stands for „Model-View-Controller“ and describes a design pattern for the development of e.g. web applications. It is a kind of implementation pattern for a multi-tier application and uses basically the well-known three-tier-architecture in software design. On the lower level there is a kind of data layer which is your data and the way to connect to your data and performing database actions. The middle tier is the so-called business logic and implements all logical elements of your application. The upper tier is the presentation layer and the only use for that tier is to display data to the user, enable the user to change or work with it and start actions on the data. Normally the presentation layer won’t use the database layer directly, it will always use the business logic layer to retrieve data and request actions to be performed.
ASP.NET MVC is implementing that kind of application design for ASP.NET web development. When creating an ASP.NET MVC application the communication between server and client will be state-less again, there is no session state saved in the servers’s responses. That means, the developer is responsible for the correct request handling. But don’t be afraid, the ASP.NET MVC framework is supporting developers with very useful functionality and makes it very easy to implement a first MVC driven web application. Say goodbye to „classic“ event handling, you won’t find that in MVC.
Although it is possible to create web applications following the MVC design pattern on your own or using the „classic“ ASP.NET web application template, ASP.NET MVC is the very strict way to enforce following the MVC design pattern in your implementation. With ASP.NET MVC 3 and its new view rendering engine Razor I’ll have a closer look at MVC. Follow me on my first steps with ASP.NET MVC.
In the next part I will describe how to create a basic ASP.NET MVC web application with Microsofts Visual Studio.