"
ASP.NET (snapshot 2017) Microsoft documentation and samples

ASP.NET MVC 3

(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 the installer here

Top Features

What’s New in ASP.NET MVC 3

Installation and Help

## ASP.NET MVC 3 Overview

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:

Extensible Scaffolding with MvcScaffold integration

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 NuGet scaffolding package called MvcScaffolding. 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:

HTML 5 Project Templates

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.

The Razor View Engine

ASP.NET MVC 3 comes with a new view engine named Razor that offers the following benefits:

Some new Razor features include the following:

Razor also includes new HTML helpers, such as the following:

For more information about Razor, see the following resources:

Support for Multiple View Engines

The Add View dialog box in ASP.NET MVC 3 lets you choose the view engine you want to work with, and the New 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 as Spark, NHaml, or NDjango.

Controller Improvements

Global Action Filters

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 the GlobalFilters collection. For more information about global action filters, see the following resources:

New “ViewBag” Property

MVC 2 controllers support a ViewData 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 the ViewBag property to accomplish the same purpose. For example, instead of writing ViewData["Message"]="text", you can write ViewBag.Message="text". You do not need to define any strongly-typed classes to use the ViewBag 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 the ViewData dictionary. (Note: in most pre-release versions of MVC 3, the ViewBag property was named the ViewModel property.)

New “ActionResult” Types

The following ActionResult types and corresponding helper methods are new or enhanced in MVC 3:

JavaScript and Ajax Improvements

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 the jQueryValidate plugin by default. If you want MVC 2 behavior, you can disable unobtrusive JavaScript using a web.config file setting. For more information about JavaScript and Ajax improvements, see the following resources:

Client-Side Validation Enabled by Default

In earlier versions of MVC, you need to explicitly call the Html.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 the web.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.

Remote Validator

ASP.NET MVC 3 supports the new RemoteAttribute 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, the Remote attribute specifies that client validation will call an action named UserNameAvailable on the UsersController class in order to validate the UserName field.

[!code-csharpMain]

   1:  public class User 
   2:  {
   3:      [Remote("UserNameAvailable", "Users")]
   4:      public string UserName { get; set; }
   5:  }

The following example shows the corresponding controller.

[!code-csharpMain]

   1:  public class UsersController 
   2:  { 
   3:      public bool UserNameAvailable(string username) 
   4:      { 
   5:          if(MyRepository.UserNameExists(username)) 
   6:          { 
   7:              return "false"; 
   8:          } 
   9:          return "true"; 
  10:      } 
  11:  }

For more information about how to use the Remote attribute, see How to: Implement Remote Validation in ASP.NET MVC in the MSDN library.

JSON Binding Support

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 the JavaScript and AJAX Improvements section of Scott Guthrie’s MVC 3 Preview blog post.

Model Validation Improvements

“DataAnnotations” Metadata Attributes

ASP.NET MVC 3 supports DataAnnotations metadata attributes such as DisplayAttribute.

“ValidationAttribute” Class

The ValidationAttribute class was improved in the .NET Framework 4 to support a new IsValid 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 new CompareAttribute attribute lets you compare the values of two properties of a model. In the following example, the ComparePassword property must match the Password field in order to be valid.

[!code-csharpMain]

   1:  public class User
   2:  { 
   3:      [Required]
   4:      public string Password { get; set; } 
   5:      [Required, Compare("Password")] 
   6:      public string ComparePassword { get; set; } 
   7:  }

Validation Interfaces

The IValidatableObject 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 the IValidatableObject interface when model binding, and automatically flags or highlights affected fields within a view using the built-in HTML form helpers.

The IClientValidatable 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 the Model Validation Improvements section of Scott Guthrie’s MVC 3 Preview blog post. (However, note that the reference to “IValidateObject” in the blog should be “IValidatableObject”.)

Dependency Injection Improvements

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 the Common Service Locator library and any DI container that supports that library’s IServiceLocator interface. It also supports a new IDependencyResolver interface that makes it easier to integrate DI frameworks.

For more information about DI in MVC 3, see the following resources:

Other New Features

NuGet Integration

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 the NuGet documentation.

Partial-Page Output Caching

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 the Partial Page Output Caching section of Scott Guthrie’s blog post on the MVC 3 release candidate and the Child Action Output Caching section of the MVC 3 Release Notes.

Granular Control over Request Validation

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 an AllowHtml 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:

Extensible “New Project” Dialog Box

In ASP.NET MVC 3 you can add project templates, view engines, and unit test project frameworks to the New Project dialog box.

Template Scaffolding Improvements

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 the Html.EditorFor helper instead of the Html.TextBoxFor helper. This improves support for metadata on the model in the form of data annotation attributes when the Add View dialog box generates a view.

New Overloads for “Html.LabelFor” and “Html.LabelForModel”

New method overloads have been added for the LabelFor and LabelForModel helper methods. The new overloads enable you to specify or override the label text.

Sessionless Controller Support

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, see MVC 3 Release Notes.

New “AdditionalMetadataAttribute” Class

You can use the AdditionalMetadata attribute to populate the ModelMetadata.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:

[!code-csharpMain]

   1:  public class ProductViewModel 
   2:  {
   3:      [AdditionalMetadata("AdminOnly", true)]
   4:      public string RefundCode {get; set;}
   5:  }

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.

AccountController improvements

The AccountController in the Internet project template has been greatly improved.

New Intranet Project Template

A new Intranet Project Template is included which enables Windows Authentication and removes the AccountController.



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/mvc3.htm
< THANKS ME>