Asp.Net MVC overview
by Pierre Boisvenue. July 27, 2010
|
Introduction
What is Asp.Net MVC and why should you care. First MVC stands for Model View Controller and it is a major shift unlike Asp.Net webforms introduced just a few years ago.
It is a downloadabled add on for Visual Studio 2008 SP1 and built in Visual Studio 2010. It is new frameworks that can co exist side by side with Asp.Net Webforms however without the drag and drop of server controls. MVC is extensible, scalable and more
lightweight that a traditional Asp.net web application. There is a clear separation of logic, business rules, presentation, test driven and validation.
Also we lose code behind files are back to using a single ASPX page for both code and HTML. All HTTP request in MVC are handled by a Controller that interacts with a Model then serve a View.
|
Visual Studio 2008 ASP.Net MVC project NerdDinner
By default upon creation of an MVC project the following directory structures gets created.
For those of you that will be using the NerdDinner solution to learn Asp.Net MVC and experience assembly
reference problems it is most likely due to the associated test project. Simply delete the unit test project
and see if the web app loads. Then it is a simple matter to create a new unit test for the MVC project.
|
Visual Studio 2008 ASP.Net MVC test unit

|
Visual Studio 2008 ASP.Net MVC Initial Load

|
Visual Studio 2008 ASP.Net MVC Initial Routing

By convention, it is implied, that when a controller is defined in a route in global.asax, that it will be associated with a filename ending in controller.

Also in a route, a controller action is defined

|
Visual Studio 2008 ASP.Net MVC Action Result
The return view is a helper
method, defined in the Controller base class. It returns a new ViewResult object. View-
Result is one of the many ActionResult derivatives that you can return from actions.
This ViewResult tells the framework to render a view. You have the option of providing
a name for the view, but if you don’t—as in our case—it will just use the name
of the action. ViewData is one way of
passing data over to the view

|
Visual Studio 2008 ASP.Net MVC - Add new controller
In solution explorer, add new controller by right click on controller folder.


Open controller and right click on the index action to add a corresponding view


This result in the view being created.

|
What’s New in ASP.NET MVC 2
PDF link
|
Controller request
In model-view-controller (MVC) architecture, incoming requests are handled by controllers. In ASP.NET
MVC, controllers are just simple C# classes usually inheriting from System.Web.Mvc. Each public method on a
controller is known as an action method, which means you can invoke it from the Web via some URL.

|
Model binding
Model binding, an
extremely useful feature of ASP.NET MVC whereby incoming data is automatically parsed and used to
populate action method parameters by matching incoming key/value pairs with the names of properties
on the desired .NET type. This powerful, customizable mechanism eliminates much of the humdrum plumbing associated
with handling HTTP requests, letting you work primarily in terms of strongly typed .NET objects rather
than low-level fiddling with Request.Form[] and Request.QueryString[] dictionaries, as is often
necessary in Web Forms

|
Strongly typed view
Strongly typed view




|
Model with validation
Model with validation

Controller now checking for IsValid

View now display validation errors via HTML helper


As an added bonus because of model binding MVC retain values

With style sheet included in view

|
MVC Architecture

Three-tier architecture
|