Introduction to ASP.NET MVC 3

ASP.NET MVC is a framework for building web application that applies the general Model View Controller pattern to the ASP.NET framework.
ASP.NET MVC 3  provides us with some of the main features listed below :

1. Expressive Views including the new Razor View Engine.

2. .NET 4 Data Annotation Support.
3. Streamlined validation with improved Model validations.
4. Powerful support of DependencyResolution and Global Action Filters.
5. Rich JavaScript support with unobtrusive JavaScript, jQuery Validations, and JSON binding.
6. Greater flexibility to maintain consistent look and feel across views with  Layouts.

Razor View Engine :

Razor is the first major update to rendering HTML since ASP.NET 1.0. The default view engine used in MVC 1 and MVC 2 was commonly called Web Forms View Engine as it uses the same ASPX/ASCX/MASTER files and syntax used in Web Forms.

For Example :

    Layout = null;

<!DOCTYPE html>

<h1>Upload Image</h1>
<div id="UploadPictureForm">
@using (Html.BeginForm("UploadImage", "Image", FormMethod.Post, new { @enctype = "multipart/form-data", id = "profilePictureForm" }))
    <div id="errorMessage"></div>
            <input style="cursor:pointer;" type="file" name="file" id="file"/><br />
    <input type="submit" style="cursor:pointer;margin-top:30px;" id="upload" value="Upload" />
@Html.ActionLink("Click to Render Image", "RenderImageBytes")

Razor was designed specifically as a view engine syntax. It has one main focus :  code-focused templating for HTML generation. The Razor syntax is easier to type and read. Razor does not have XML - like heavy syntax of the Web Forms view engine.

Razor syntax has following advantages and features :

1. Compact, expressive, and fluid: Razor syntax makes very simple to express your coding intent. Razor also simplifies markup with an improvement on the Master Pages concept called Layouts. Layouts are more flexible and and requires less code. The @ character is used to signify the transition from markup to code, and the Razor engine automatically detected the transition back to markup.

2. Not a new Language: Razor is not a new language. It is a syntax that lets you use your existing .NET coding skills in a template in a very intuitive way.

3. Easy to learn: As Razor is not a new language, it is easy to learn. You need to know HTML, .NET and need to type HTML and hit the @ sign whenever you need to write some .NET code.

4. Works with any text editor: Razor is so lightweight and HTML-focused, we can use any text editor of our choice. Visual studio's syntax highlighting and intelliSense features are nice, but it's simple enough that you can edit it in any text editor.

.NET 4 Data Annotation Support:
Validation is an important part of building web application. MVC 2 was compiled against .NET 3.5 and thus did not support any of the .NET 4 Data Annotations enhancements. MVC 3 picks up some new, very useful validtion features available due to new .NET 4 support. Some example include:

1. The DisplayName attribute was not localizable in MVC 2 whereas the .NET 4 standard System.ComponentModel.DataAnnotations Display attribute is localizable.

2. ValidationAttribute was enhanced in .NET 4 to better work with the validation context for the entire model, greatly simplifying cases like validators that compare or otherwise reference two model properties.

Unobtrusive JavaScript: 

The Unobtrusive Javascript does not affect your page markup. The Unobtrusive JavaScript attaches to elements by their ID or class rather than with event attributes like onclick and onsubmit. 

JSON Binding:
MVC3 includes JSON (JavaScript Object Notation) binding support via the new JsonValueProviderFactory, enabling your action methods to accept and model-bind data in JSON format. This is especially useful in advanced AJAX scenarios like client templates and data binding that need to post data back to the server.

Dependency Resolution: 

ASP.NET MVC 3 introduces a new concept called a dependency resolver, which greatly simplifies the use of dependency injection in your application. This makes it easier to decouple application components.

Support has been added for following scenarios:

1. Controllers (registering and injecing controller factories, registering controllers)
2. Views (registering and injecting view engines, injecting dependencies into view pages)
3. Action Filters (locating and injecting filters)
4. Model binders (registering and injecting)
5. Model Validation providers (registering and injecting)
6. Model metadata roviders (registering and injecting)
7. Value providers (registering and injecting)

Global Action Filters:

MVC 2 action filters provided hook to execute code before or after an action method ran. They were implemented as custom attribute that could be applied to controller action or to an entire controller.
MVC 3 extends this with global action filters, which apply to all action method in the application. This is especially useful for application infrastructure concerns like error handling and logging.

MVC Application Structure:

When you create a new ASP.NET MVC 3 application it automatically adds several files and directories to the project. The ASP.NET MVC 3 project by default have five top level directories as shown below:

1. /Controllers: where we put controller classes that handles URL request.
2. /Models: where you put classes that represents and manipulate data and        business objects.
3. /Views: where you put UI template files that are responsible for rendering     output, such as HTML.
4. /Scripts: where you put javaScript library files and scripts (.js).
5. /Content: where you put CSS and image files, and other non-dynamic/non-     JavaScript content.

ASP.NET MVC 3 and Conventions:
ASP.NET MVC application, by default, rely heavily on conventions. This allows developers with already configure conventions.

For Example: MVC uses a convention-based directory naming structure when resolving View template, and this convention allows you to omit the location path when referencing the View from within a controller class. By default, ASP.NET MVC looks for the View template file within the \Views\[ControllerName]\directory underneath the application.

Convention over Configuration:
The convention over configuration concept was made popular by Ruby on Rails. It essentially means :

We know, by now, how to build a web application. Let's roll that experience into the framework so we don't have to configure absolutely everything again.

We can see this concept at work in ASP.NET MVC 3 by taking look at three core directories that make the application work:

2. Models

We don't need to set these folder names in the web.config file, they are expected to be there by convention. This saves us the work of having to edit an XML file like web.config, for example, in order to tell MVC engine that you can find my View in View directory. It knows it by convention.

ASP.NET MVC's conventions are pretty straight forward. 

1. Each Controller's class name ends with Controller.
      eg: HomeController
2. There is a single View directory for all the views of the application.
3. Views that controller uses live in a subdirectory of the Views main directory and the subdirectory is named according to controller name.
    eg: The views for the HomeController would live in /Views/Home.
4. All reusable UI elements live in a similar structure i.e. shared directory in the Views folder.


Layout in MVC 3 Razor helps in maintaining consistent look and feel across the multiple views within your application. Layouts serve the same purpose as Master pages, but offers simpler syntax and greater flexibility.
We can use Layout to define a common template. This template contains one or more placeholders that the other views in our application provide content for. In some ways, it is like an abstract base class for our Views.


Post a Comment