📜 ⬆️ ⬇️

ASP.NET MVC 3: a detailed overview of the innovations


More than four months after the release of the final version of the ASP.NET MVC 2 framework, the development team is pleased to present the first preview version of ASP.NET MVC 3 . An announcement and download link can be found here .

This article will provide an overview of the innovations and changes that have occurred in the new version of the framework.

Compatibility and support for .NET Framework 4.0

Unlike the previous version, ASP.NET MVC 3 supports only the fourth version of the .NET Framework . In addition, developer components for integration into Visual Studio are supported only for Visual Studio 2010 and Visual Web Developer Express 2010 .

The ASP.NET MVC 3 framework can be installed together with ASP.NET MVC 2, they can be used separately from each other.

Upgrade from previous version to ASP.NET MVC 3

The simplest way to update projects from previous versions of the framework to a new preliminary is to create a new MVC 3 project and transfer all the old project components into it: controllers, views, auxiliary files.

To manually update projects based on ASP.NET MVC 2 to a new version of ASP.NET MVC 3 Preview 1, you need to do the following:

Innovations in ASP.NET MVC 3 Preview 1

The new version of the framework has a number of important innovations.

Razor View Engine Support

Starting with this version, ASP.NET MVC introduces support for new Razor view markup syntax. Not so long ago, Razor was presented in the form of the basic syntax of representations in the project of WebMartix web development tools . Razor is now also available for developing MVC applications.

Razor is an optimized syntax for working with HTML code. Web Forms engine in ASP.NET focuses much on controls, and its code looks overloaded among HTML code. On the contrary, Razor offers a highly optimized solution that allows you to concisely embed the presentation logic in the HTML code.

Consider an example. Below is the form built using the old presentation engine (left) and the new Razor engine (right).


The main difference Razor, which catches the eye - is the lack of opening and closing sets "<%%". Indeed, even the @using grouping syntax contains no closing elements and is represented only by the trailing “}” character. Compare this with the need to write “<%}%>” in the WebForms version and you will understand what the main focus of Razor development is - developer productivity through reducing the code and its conciseness.

Especially, the convenience of Razor manifests itself when creating branches in code, for example, this is how some code looks like in WebForms and Razor:


It is very tiring to ramify branches that require you to beat off the conditions from the markup with the elements “<%%>”. On the other hand, look how harmoniously the branching markup looks when using Razor. The same advantage is demonstrated in many other cases, such as cycles.

In Razor, there are differences not only in the different syntax of opening and closing characters. So, for example, below is the configuration code for the page and its header for WebFroms:


The same code, but in the style of Razor is presented below:


Much more details about the Razor view engine can be found in this translation of Scott Guthrie's article.

Multiple view engine support in Visual Studio

With the creation of the new Razor presentation engine, MVC developers have been concerned about giving the developer the choice of which mechanism he will use. Visual Studio tools now have the ability to select the view engine when creating a new view.


Dynamic View and ViewModel Properties

Since MVC3 only supports .NET 4.0, the framework developers were able to take advantage of the new version of .NET in ASP.NET MVC.

One of these advantages is the support of new dynamic types. In MVC 3, the familiar ViewData property is represented as two dynamic implementations, View and ViewModel . This allows you instead of the following code:

ViewData ["Title"] = "The Title"; </ em
ViewData ["Message"] = "Hello World!";

initialize the values ​​differently:

ViewModel.Title = "The Title";
ViewModel.Message = "Hello World!";

The ability to create extensions for objects on the fly is a property of a dynamic type in .NET 4.0. Access to the fields in the context of the view in this case will be simplified:

<h2> View.Title </ h2>
<p> View.Message </ p>

Dependency Injection Support

In MVC 3, developers have increased the scope for expanding the framework and added support for injecting code out of the box without having to resort to their own implementations.

In ASP.NET MVC 3 Preview 1, the ability to inject in the following locations has been added:To implement the injection, the developer must determine the implementation of the IServiceLocator interface:


After that, the developer registers a new instance using a global static class, which is defined as follows:

The developer calls the SetCurrent method to set up a new implementation of the IServiceLocator .

After adding a new implementation, ASP.NET MVC 3 will use it for the following purposes:In the next versions of ASP.NET MVC 3, additional code injection options will appear:In connection with the introduction of support for code injection, the following changes were made to the behavior of standard ASP.NET MVC mechanisms:

Global filters

In MVC 3, a global filter mechanism has been added that will allow you to define filters that will be invoked each time you call actions of any controller. To add a filter, use the global static class GlobalFilters :

GlobalFilters.Filters.Add (new MyActionFilter ());

You can override the global filter mechanism by creating an injection for the new IFilterProvider interface. This will allow you to create your own global filter execution logic.

JsonValueProviderFactory Value Provider

The new value provider will allow your actions to receive requests with parameter sets in JSON format and match them with the parameters of the action method. Previously, this functionality was located in the library ASP.NET MVC Futures .

For example, if a POST request with an application / json MIME type contains the value:

{“ProductName”: “Milk”, “Cost”: “12.0”}

Then, using the JsonValueProviderFactory provider , these values ​​will automatically be assigned to the parameter with the ProductModel type in the following action:

public ActionResult SaveProduct (ProductModel productModel)

where the type ProductModel is declared as

public class ProductModel
publi string ProductName {get; set;}
public string Cost {get; set;}

Support for .NET 4.0 validation attributes and IValidatableObject interface

The ValidationAttribute class was updated in .NET 4.0 and extended by the new overloaded IsValid method with a parameter of type ValidationContext . This parameter, besides the new property value, contains the context of the validation and an instance of the validation object. This will allow you to check not only the current property, but also other properties of the object and validate based on their values.


The example above demonstrates the comparison of two properties of the object passed in the context of validation.

In addition, a new interface, IValidatableObject , has been added to .NET 4.0, which will allow you to define validation logic at the class level of your model.


The model defined above implements the IValidatableObject interface for its own validation. MVC 3 supports this interface and validates the model based on it.

IClientValidatable interface

This new interface is declared as


It is designed to provide third-party frameworks validation information on customer validation support.

Support for .NET 4 metadata attributes

MVC 3 supports new metadata attributes that were introduced in .NET 4. For example, DisplayAttribute. A complete list of new attributes that are actually used in MVC 3 cannot yet be provided, there is no information on this subject, and the source codes of the project are not yet available.


The new interface is defined as


It is designed to be able to determine the moment of creation of metadata for the model and the ability to perform additional actions at this moment. This interface is used by classes inherited from the AssociatedMetadataProvider, for example, the DataAnnotations - DataAnnotationsModelMetadataProvider metadata class.

New types of results of actions

In MVC 3, a number of new types have appeared for returning the results of actions.


A simple action result type that returns a result indicating that the requested resource was not found (HTTP 404). The controllers contain the new helper method HttpNotFound (), which returns the type HttpNotFoundResult :



New class declared as


The HttpStatusCodeResult class is designed to return an arbitrary HTTP code as the result of an action.

As you can see, it contains two constructors that accept the digital value of the HTTP code and the second additionally accepts a string describing the result.

Constant redirect

In MVC3 added support for returning the results of actions in the form of a permanent redirect (HTTP 301). This can be done using the following new controller methods:

Changes incompatible with MVC2

The order for executing exception filters has been changed. Previously, the exception filters applied to the controller were executed earlier than the filters applied to the action (if they had the same Order value). Now and further, this behavior is changing. The first will be to execute exception filters for actions, and after them filters applied to controllers. If the Order value of these filters is different, then they will be executed as before according to the order defined in Order.

Known Issues

This release has a number of issues that will be fixed in future releases:


As a result of the first preliminary release, you can confidently say that the new version of ASP.NET MVC 3 will bring a lot of new features. The functionality is already available for continuous study and I think that with subsequent releases the number of new features will only increase.

That is why, for the study and testing of the new functionality, preliminary versions are released in parts. I urge all ASP.NET MVC developers to pay close attention to the release of MVC3 Preview 1 and devote their time to exploring its new features.

Source: https://habr.com/ru/post/100380/

All Articles