August 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

ASP.NET MVC 101

  • December 3, 2008
  • By Jani Järvinen
  • Send Email »
  • More Articles »

During the years that classic ASP and ASP.NET have existed, developers have learned to build applications based on web forms, session state, and server-side controls. But, the ability to dynamically route web server calls, create applications that separate logic from the user interface, and be easy to unit test are harder to build.

For these reasons, Microsoft is preparing an alternative to "traditional" ASP.NET development. This new technique is called ASP.NET MVC, short for the well-known model-view-controller pattern. Currently, ASP.NET MVC is in the public beta stage, with Beta 1 from October 2008 being the latest available version.

When designing ASP.NET MVC, Microsoft has focused on four main areas:

  • Clear separation of concerns
  • Support for Test-Driven Development (TDD)
  • Fine-grained control over HTML and JavaScript
  • Intuitive URLs

Before showing you ASP.NET MVC, let me provide a word of caution. The code listings in this article are based on Beta 1. So, they might not directly work once the final version comes out.

Separating Logic from the Representation

The MVC pattern was designed to help developers write applications that have the user interface (the representation, or the view) separated from the logic of the application. In the MVC pattern's case, the logic lives in the controller, which in turn can access data stored in the model. The model is a representation of a data object, such as a class holding information about a customer. Commonly, the model's data is read from a database.

Although this can sound complex, the pattern has many advantages. The only real drawbacks are increased complexity and (initial) development time. On the other hand, advantages include the possibility to make quick changes later; for example, to convert a GUI application into a web application. If properly done, only the view needs to change. Put another way, the parts of the application are loosely coupled, which is a design goal in many modern applications.

Other advantages include the ability to better unit test parts of the application, because for example the controller and model can be separately tested. Finally, the MVC pattern supports code re-use, because the same controller and model code often can be used almost without modifications in new projects.

When it comes to ASP.NET MVC, the MVC separation is used, but it is not enforced in any way. Thus, you can write ASP.NET MVC applications that do not follow the pattern, but if you decide not to follow it, you should have a good reason in doing so.

Understanding MVC Applications

To begin developing ASP.NET MVC applications, you should first download and install the latest ASP.NET MVC beta. Visual Studio's New Project dialog box should now have a new icon for "ASP.NET MVC Application" (see Figure 1).



Click here for a larger image.

Figure 1: After installing the ASP.NET MVC beta, you will see the new project icons in Visual Studio.

When you create a new ASP.NET MVC project, you will have several folders under the solution: namely Controllers, Models, and Views (see Figure 2). When you write code to implement your application, you should store each of the different code files into their correct folders. Otherwise, the system might not work as designed.

Figure 2: ASP.NET MVC applications use multiple folders.





Page 1 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel