
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:
- replace the System.Web.Mvc, Version = 2.0.0.0 lines with the System.Web.Mvc, Version = 3.0.0.0 lines in all web.config files;
- remove from the project a link to the System.Web.Mvc build of the second version and add the build of the third version of the framework;
- edit the project .csproj files, replacing the value of the ProjectTypeGuids element from {F85E285D-A4E0-4152-9332-AB1D724D3325} to {E53F8FEA-EAE0-44A6-8774-FFD645390401} ;
- If the project refers to assemblies compiled for ASP.NET MVC 2, add the following code to the configuaton section of the web.config file:

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:

![image [40] image[40]](http://microgeek.ru/upload/blog/aspnet/a7b/a7bfce70692963a046ca55754f9aa940.png)
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:
- creation of a factory of controllers;
- creation of controllers;
- different stages of the work of the presentation engines, both Web Forms and Razor;
- operation of action filters
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:
- instantiating the controller factory and the controllers themselves;
- instantiating the view engines and the view pages themselves;
- instantiating action filters.
In the next versions of ASP.NET MVC 3, additional code injection options will appear:
- for the Model Binder mechanism;
- for value providers (Value Providers);
- for validation providers;
- for providers of metadata models.
In connection with the introduction of support for code injection, the following changes were made to the behavior of standard ASP.NET MVC mechanisms:
- The ControllerBuilder.GetControllerFactory will first refer to the existing IServiceLocator implementation and only then, if there is no implementation, use the old controller factory creation mechanism;
- When creating controllers, the DefaultControllerFactory will first refer to the existing IServiceLocator implementation and only then, if there is no implementation, use the old mechanism for creating controllers;
- the DefaultControllerFactory.ReleaseController function - the controller is released - will be performed only through the IServiceLocator implementation, if there is no implementation, the default implementation from the special interface IMvcServiceLocator will be used;
- ViewEngineCollection , as before, will allow adding collections of view engines, but in addition to MVC 3 this collection will virtually contain all view engines registered in the IServiceLocator implementation. What means call method. FindView or. FindPartialView will return generalized values, both manually added and those defined using the IServiceLocator implementation;
- The WebFormView has been updated, so that pages and controls are created using the IServiceLocator implementation or the old way through Activator.CreateInstance , if the page can not be created through the IServiceLocator implementation.
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:
[HttpPost]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.
IMetadataAware
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.
HttpNotFoundResult
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 :

HttpStatusCodeResult
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:
- RedirectPermanent - performs redirection to the specified URL;
- RedirectToRoutePermanent - performs redirection along the current route (or you can specify optional parameters routeName and routeValues );
- RedirectToActionPermanent — redirects to the specified action (you can specify the optional parameters controllerName and routeValues ).
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:
- No syntax highlighting and IntelliSense support in Visual Studio for the Razor view engine;
- It is not possible to use the Go To Controller function when editing files. cshtml (Razor);
- There is no access to Visual Studio snippets when editing code with the Razor view engine.
Conclusion
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.