This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
(includes April 2011 Tools Update)
ASP.NET MVC 3 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework.
It installs side-by-side with ASP.NET MVC 2, so get started using it today!
Download theinstaller here
What's New in ASP.NET MVC 3
Installation and Help
ASP.NET MVC 3 builds on ASP.NET MVC 1 and 2, adding great features that both simplify your code and allow deeper extensibility. This topic provides an overview of many of the new features that are included in this release, organized into the following sections:
The new Scaffolding system makes it easier to pick up and start using productively if you're entirely new to the framework, and to automate common development tasks if you're experienced and already know what you're doing.
This is supported by new NuGetscaffolding package calledMvcScaffolding. The term "Scaffolding" is used by many software technologies to mean "quickly generating a basic outline of your software that you can then edit and customize". The scaffolding package we're creating for ASP.NET MVC is greatly beneficial in several scenarios:
Other features in MvcScaffolding include:
The ASP.NET MVC 3 Tools Update includes great Visual Studio support for this scaffolding system, such as:
For more information about Scaffolding in ASP.NET MVC 3, see the following resources:
Steve Sanderson's post series, including:
Scott Hanselman's post from his PDC 2010 sessionBuilding a Blog with Microsoft "Unnamed Package of Web Love"
The New Project dialog includes a checkbox enable HTML 5 versions of project templates. These templates leverage Modernizr 1.7 to provide compatibility support for HTML 5 and CSS 3 in down-level browsers.
ASP.NET MVC 3 comes with a new view engine named Razor that offers the following benefits:
Some new Razor features include the following:
@model
syntax for specifying the type being passed to the view.@* *@
comment syntax.layoutpage
) once for an entire site.Html.Raw
method for displaying text without HTML-encoding it.Razor also includes new HTML helpers, such as the following:
Chart
. Renders a chart, offering the same features as the chart control in ASP.NET 4.WebGrid
. Renders a data grid, complete with paging and sorting functionality.Crypto
. Uses hashing algorithms to create properly salted and hashed passwords.WebImage
. Renders an image.WebMail
. Sends an email message.For more information about Razor, see the following resources:
TheAdd View dialog box in ASP.NET MVC 3 lets you choose the view engine you want to work with, and theNew Project dialog box lets you specify the default view engine for a project. You can choose the Web Forms view engine (ASPX), Razor, or an open-source view engine such asSpark,NHaml, orNDjango.
Sometimes you want to perform logic either before an action method runs or after an action method runs. To support this, ASP.NET MVC 2 provided action filters. Action filters are custom attributes that provide a declarative means to add pre-action and post-action behavior to specific controller action methods. However, in some cases you might want to specify pre-action or post-action behavior that applies to all action methods. MVC 3 lets you specify global filters by adding them to theGlobalFilters
collection. For more information about global action filters, see the following resources:
MVC 2 controllers support aViewData
property that enables you to pass data to a view template using a late-bound dictionary API. In MVC 3, you can also use somewhat simpler syntax with theViewBag
property to accomplish the same purpose. For example, instead of writingViewData["Message"]="text"
, you can writeViewBag.Message="text"
. You do not need to define any strongly-typed classes to use theViewBag
property. Because it is a dynamic property, you can instead just get or set properties and it will resolve them dynamically at run time. Internally,ViewBag
properties are stored as name/value pairs in theViewData
dictionary. (Note: in most pre-release versions of MVC 3, theViewBag
property was named theViewModel
property.)
The followingActionResult
types and corresponding helper methods are new or enhanced in MVC 3:
RedirectPermanent
,RedirectToRoutePermanent
, andRedirectToActionPermanent
. These methods return an instance ofRedirectResult
with thePermanent
property set totrue
.By default, Ajax and validation helpers in MVC 3 use an unobtrusive JavaScript approach. Unobtrusive JavaScript avoids injecting inline JavaScript into HTML. This makes your HTML smaller and less cluttered, and makes it easier to swap out or customize JavaScript libraries. Validation helpers in MVC 3 also use thejQueryValidate
plugin by default. If you want MVC 2 behavior, you can disable unobtrusive JavaScript using aweb.config file setting. For more information about JavaScript and Ajax improvements, see the following resources:
In earlier versions of MVC, you need to explicitly call theHtml.EnableClientValidation
method from a view in order to enable client-side validation. In MVC 3 this is no longer required because client-side validation is enabled by default. (You can disable this using a setting in theweb.config file.)
In order for client-side validation to work, you still need to reference the appropriate jQuery and jQuery Validation libraries in your site. You can host those libraries on your own server or reference them from a content delivery network (CDN) like the CDNs from Microsoft or Google.
ASP.NET MVC 3 supports the newRemoteAttribute class that enables you to take advantage of the jQuery Validation plug-in's remote validator support. This enables the client-side validation library to automatically call a custom method that you define on the server in order to perform validation logic that can only be done server-side.
In the following example, theRemote
attribute specifies that client validation will call an action namedUserNameAvailable
on theUsersController
class in order to validate theUserName
field.
public class User { [Remote("UserNameAvailable", "Users")] public string UserName { get; set; }}
The following example shows the corresponding controller.
public class UsersController { public bool UserNameAvailable(string username) { if(MyRepository.UserNameExists(username)) { return "false"; } return "true"; } }
For more information about how to use theRemote
attribute, seeHow to: Implement Remote Validation in ASP.NET MVC in the MSDN library.
ASP.NET MVC 3 includes built-in JSON binding support that enables action methods to receive JSON-encoded data and model-bind it to action-method parameters. This capability is useful in scenarios involving client templates and data binding. (Client templates enable you to format and display a single data item or set of data items by using templates that execute on the client.) MVC 3 enables you to easily connect client templates with action methods on the server that send and receive JSON data. For more information about JSON binding support, see theJavaScript and AJAX Improvements section ofScott Guthrie's MVC 3 Preview blog post.
ASP.NET MVC 3 supportsDataAnnotations
metadata attributes such asDisplayAttribute
.
TheValidationAttribute
class was improved in the .NET Framework 4 to support a newIsValid
overload that provides more information about the current validation context, such as what object is being validated. This enables richer scenarios where you can validate the current value based on another property of the model. For example, the newCompareAttribute
attribute lets you compare the values of two properties of a model. In the following example, theComparePassword
property must match thePassword
field in order to be valid.
public class User{ [Required] public string Password { get; set; } [Required, Compare("Password")] public string ComparePassword { get; set; } }
TheIValidatableObject interface enables you to perform model-level validation, and it enables you to provide validation error messages that are specific to the state of the overall model, or between two properties within the model. MVC 3 now retrieves errors from theIValidatableObject
interface when model binding, and automatically flags or highlights affected fields within a view using the built-in HTML form helpers.
TheIClientValidatable interface enables ASP.NET MVC to discover at run time whether a validator has support for client validation. This interface has been designed so that it can be integrated with a variety of validation frameworks.
For more information about validation interfaces, see theModel Validation Improvements section ofScott Guthrie's MVC 3 Preview blog post. (However, note that the reference to "IValidateObject" in the blog should be "IValidatableObject".)
ASP.NET MVC 3 provides better support for applying Dependency Injection (DI) and for integrating with Dependency Injection or Inversion of Control (IOC) containers. Support for DI has been added in the following areas:
MVC 3 supports theCommon Service Locator library and any DI container that supports that library'sIServiceLocator
interface. It also supports a newIDependencyResolver
interface that makes it easier to integrate DI frameworks.
For more information about DI in MVC 3, see the following resources:
ASP.NET MVC 3 automatically installs and enables NuGet as part of its setup. NuGet is a free open-source package manager that makes it easy to find, install, and use .NET libraries and tools in your projects. It works with all Visual Studio project types (including ASP.NET Web Forms and ASP.NET MVC).
NuGet enables developers who maintain open source projects (for example, projects like Moq, NHibernate, Ninject, StructureMap, NUnit, Windsor, RhinoMocks, and Elmah) to package their libraries and register them in an online gallery. It is then easy for .NET developers who want to use one of these libraries to find the package and install it in projects they are working on.
With the ASP.NET 3 Tools Update, project templates include JavaScript libraries pre-installed NuGet packages, so they are updatable via NuGet. Entity Framework Code First is also pre-installed as a NuGet package.
For more information about NuGet, see theNuGet documentation.
ASP.NET MVC has supported output caching of full page responses since version 1. MVC 3 also supports partial-page output caching, which allows you to easily cache regions or fragments of a response. For more information about caching, see thePartial Page Output Caching section ofScott Guthrie's blog post on the MVC 3 release candidate and theChild Action Output Caching section of theMVC 3 Release Notes.
ASP.NET MVC has built-in request validation that automatically helps protect against XSS and HTML injection attacks. However, sometimes you want to explicitly disable request validation, such as if you want to let users post HTML content (for example, in blog entries or CMS content). You can now add anAllowHtml attribute to models or view models to disable request validation on a per-property basis during model binding. For more information about request validation, see the following resources:
In ASP.NET MVC 3 you can add project templates, view engines, and unit test project frameworks to theNew Project dialog box.
ASP.NET MVC 3 scaffolding templates do a better job of identifying primary-key properties on models and handling them appropriately than in earlier versions of MVC. (For example, the scaffolding templates now make sure that the primary key is not scaffolded as an editable form field.)
By default, the Create and Edit scaffolds now use theHtml.EditorFor
helper instead of theHtml.TextBoxFor
helper. This improves support for metadata on the model in the form of data annotation attributes when theAdd View dialog box generates a view.
New method overloads have been added for theLabelFor
andLabelForModel
helper methods. The new overloads enable you to specify or override the label text.
In ASP.NET MVC 3 you can indicate whether you want a controller class to use session state, and if so, whether session state should be read/write or read-only. For more information about sessionless controller support, seeMVC 3 Release Notes.
You can use theAdditionalMetadata attribute to populate theModelMetadata.AdditionalValues
dictionary for a model property. For example, if a view model has a property that should be displayed only to an administrator, you can annotate that property as shown in the following example:
public class ProductViewModel { [AdditionalMetadata("AdminOnly", true)] public string RefundCode {get; set;}}
This metadata is made available to any display or editor template when a product view model is rendered. It is up to you to interpret the metadata information.
The AccountController in the Internet project template has been greatly improved.
A new Intranet Project Template is included which enables Windows Authentication and removes the AccountController.
Was this page helpful?
Was this page helpful?