ASP.Net MVC is a .Net framework that makes it easy to implement the MVC (Model, View, Controller) pattern in your own .NET applications. It is an alternative to, not a replacement, standard ASP.Net web forms.
You can download and install MVC for .Net at http://www.microsoft.com/web/downloads or from http://www.asp.net. These notes have been compiled while reviewing the NerdDinner project and reading ASP.Net MVC 1.0 which has a free first chapter at http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx . These notes are deliberately brief.
Model
Objects that represent the data of the application and corresponding domain logic (validation and business rules). The heart of an MVC application
Model classes may often go into a separate class library from the web application.
Can include repository classes to isolate data access logic (CRUD).
Folder Structure of Models using LINQ to SQL and a Repository Pattern (see below) to abstract the CRUD into separate classes.
-Models
---Dinner.cs (a partial class. The main part of this class is generated dynamically from NerdDinner.dbml)
---DinnerRepository.cs (Select, Insert, Update, Delete functions for Dinners)
---NerdDinner.dbml - (design surface)
------NerdDinner.dbml.layout - (xml rep of design surface)
------NerdDinner.designer.cs - (auto generated code from what is on design surface)
*Since we have validation in the Model, we don't have to update or rewrite validation code on each view.
View
Responsible for outputting the HTML representation of the data. There can be an ListView, ItemView, UpdateView, CreateView, DeleteView. There can certainly be more views, but these are standard.
-Views (folder)
---Dinners (folder named for Controller [DinnerController in this case] )
------Details.aspx
------Edit.aspx
------Index.aspx
------NotFound.aspx
Views are templates. When MVC is trying to resolve your url, it will look for Views/Controller/Function/ first, but if not found, then t will look in the Views/Shared/ folder
Controller
In most web apps, urls map to files, but in an MVC app, urls map to functions in classes. Controllers process incoming HTTP requests. As an example, www.mysite.com/dinners/edit/1 initiates the 'edit' function of the 'DinnersController' and passes a parameter of '1'. The url pattern is http://domain/controllerClass/Function/Paramenter.
-Controllers (folder)
---DinnerCointroller.cs
.......Functions:
..........Index - the default
..........Details(int id)
..........Edit(int id)
..........Edit (int id) [AcceptsVerbs(HttpVerbs.Post)]
Data Access with MVC
ASP.Net MVC framework will work with any data access technology. LINQ to SQL is used in the book, and is a simple DA solution. Using a Linq to SQL designer (dbml file) creates a DataContext class that exposes table objects as properties and handles the LINQ queries to return objects.
Repository Pattern
Instead of directly dealing with the 'DataContext' object to create queries each time we need to retrieve data from the data store, a repository pattern can be used to isolate all your data access logic. All data access methods go in the Model/xxxRepository.cs class, if we use the repository pattern described in NerdDinner.
ASP.NET MVC Hosting
All our ASP.Net hosting plans are suitable to host ASP.Net MVC website. Choose the plan based on your size requirements knowing that the features available are the same for all our hosting plans. For more details compare
ASP.Net MVC Hosting Plans.