As you may know, MVC2 is the newest version of a Model-View-Controller framework built on top of ASP.NET and .NET 3.5 SP1, and comes built-in with Visual Studio 2010.
This is an on-going series about creating simple web-sites in MVC2 and I’ll talk about some relevant Design Patterns along the way. I’ll be using C#, but the knowledge is applicable to any .NET language.

Ok, let’s get started in VS 2010. Select New Project: ASP.NET MVC2 Web Application, and Select a Name and Location for your solution. In the following step, I would highly recommend opting to Create a Project with Unit Tests.

Right off the bat, you get a Web Solution with some default folders containing Models, Views and Controllers.

mvcfolders Unlike some Java MVC frameworks and like Ruby on Rails, MVC2 follows the principle of Convention over Configuration. What this means is that the folder structure and naming of your pieces is important and should follow the required Convention, but no Configuration information is required because the Framework will automatically wire things together, which is quite convenient.

As you see, folders called Controllers, Models and Views are created. prefixes and names are important here. For e.g. an Employee controller should be called EmployeeController and be in the Controllers folder. There should be a Views folder called Employee which will contain Views for it. Default Views are named as the Action.
You can define a model (think data class) under Models.

To support a request like http://mywebsite/employee/details/12345, we need to create an Employee controller and a Details View:

Controllers
   EmployeeController.cs

Views
   Employee
      details.aspx

To create the Employee controller, right-click the Controlers folder, select Add a Controller called EmployeeController.
To create the View, right-click Views folder, Create a folder called Employee, and right Click to Add  a View called details.aspx

If you want to create a strongly-typed data class, it could be

Models
   EmployeeData.cs

So, now we have the pieces, and need to wire the http request to the Controller.
The routing is MVC2 is done in global.asax.cs with

routes.MapRoute(…)

That specifies how an http request of a given pattern will invoke a specified action (or method) on a specified Controller class with the required parameters.
You should really understand how this routing works; this is a critical part of your app, and takes a little time to wrap your head around because it is super-flexible.

e.g. To map employee/details/12345, specify:

routes.MapRoute(
    “employeeDetails”, 
    “{controller}/{action}/{id}”,
     new { controller = “Employee”, action = “Details”, id = UrlParameter.Optional } 

This means that any request in the form of employee/details/* will invoke an Action (or method) called details in the employee controller (class named EmployeeController).

Next, we’ll talk a bit about models, passing data around and the new and powerful Data Attributes…

Advertisements