ASP. net mvc 3.0 (I): summary of the new features of MVC 3.0

Source: Internet
Author: User
Document directory
  •  
  •  
  •  

ASP. net mvc 3.0 (I): summary of the new features of MVC 3.0

ASP. net mvc 3.0 (II): MVC concept and MVC 3.0 development environment

ASP. net mvc 3.0 (III): first recognized mvc url ing rules Routing

ASP. net mvc 3.0 (4): I want to configure routing for MVC rules.

ASP. net mvc 3.0 (V): Start With Controller/Action

ASP. net mvc 3.0 (6): Create your view in MVC 3.0

ASP. net mvc 3.0 (7): The New razor engine of MVC 3.0

ASP. net mvc 3.0 (8): passing and saving your model in MVC 3.0

ASP. net mvc 3.0 (9): MVC 3.0 verifies your model

ASP. net mvc 3.0 (10): MVC 3.0 uses forms authentication

ASP. net mvc 3.0 (11): Use filter in MVC 3.0

ASP. net mvc 3.0 (12): MVC 3.0 uses custom HTML controls

ASP. net mvc 3.0 (13): MVC 3.0 prevents cross-site Request Forgery (csrf) Attacks

ASP. net mvc 3.0 (14): create a data table in the MVC 3.0 instance Series

ASP. net mvc 3.0 (15th): Sorting of tables in the MVC 3.0 instance Series

ASP. net mvc 3.0 (16): pages of table data in the MVC 3.0 instance Series

ASP. net mvc 3.0 (17): filtering data in tables of MVC 3.0 instances

ASP. net mvc 3.0 (18): Combined sorting, paging, and filtering of tables in the MVC 3.0 instance Series

ASP. net mvc 3.0 (19th): using open-source controls for MVC 3.0 instances to sort and pagination tables

Summary

Through the development of MVC versions 1.0 and 2.0, it has now reached the leading age of 3.0. As technology continues to improve, MVC has become more and more mature. It also makes development concise and humanized and artistic.

Many big birds in the garden are familiar with MVC, and the 7th of Sun Wukong is quite easy and enviable. We read and read the books of laruence as a newbie, but we still don't know much about mvc3.0.

This kind of cup is more than a cup. After comprehensive analysis, we can conclude that the eyes are low !!!

Finally, cainiao decided to start learning MVC 3.0 from the very beginning and step by step. I also hope to share with me the tips of mvc3.0 ....

 

Preface

ASP. NET mvc3 adds a lot of features on the basis of ASP. net mvc 1 and 2, making the code simpler and scalable in depth. This article provides a description of many new features included in this release, which are divided into the following parts:

  • Razor view Engine
  • Support for multi-view Engines
  • Controller Improvement
  • JavaScript and Ajax
  • Improvement of Model Verification
  • Dependency injection dependency injection Improvement
  • Other new features

 

Razor view Engine

ASP. NET mvc3 brings a new view engine named Razor, which provides the following advantages:

  • The syntax of razor is simple and clear. You only need to minimize the input.
  • Razor is easy to learn. The syntax is similar to C # and VB.
  • Visual Studio provides smart prompts and syntax coloring for Razor.
  • Razor view can be tested without allowing programs or starting Web Servers

Razor now provides some new features:

  • @ Model: Specifies the model type uploaded to the view.
  • @ *** Annotation syntax
  • For the entire site, you can set the default project at one time, such as layout.
  • The HTML. Raw method provides output without HTML encoding.
  • Supports code sharing among multiple views (_ viewstart. cshtml or _ viewstart. vbhtml)

Razor also contains the new HTML helper, for example:

  • Chart. Generate chart
  • Webgrid: generates data tables and supports complete paging and sorting.
  • Crypto, which uses the hash algorithm to create the hash and salt-adding passwords
  • Webimage to generate images
  • Webmail: send an email

Sample Code:

 

 

 
@model List<MvcMusicStore.Models.Album>
@{
ViewBag.Title = "ASP.NET MVC Music Store";
}
<div id="promotion">
</div>


<ul id="album-list">
@foreach (var album in Model)
{
<li><a href="@Url.Action("Details", "Store",
new { id = album.AlbumId })">


<span>@album.Title</span></a>
</li>
}
</ul>
 

 

 

For more information about Razor, refer to the following resources:

  • Scott Guthrie's blog post introducing razor
  • Scott Guthrie's blog post introducing the @ model keyword
  • Scott Guthrie's blog post introducing razor layouts
  • Razor API Quick Reference
  • MVC 3 release notes
Support for multi-view Engines

In ASP. in. Net mvc3, The add view dialog box allows you to select the view engine you want. In the new project dialog box, you can specify the default view engine of the project. You can select webform, razor, or open-source view engines, such as spark, nhaml, or ndjango.

Select view engine:

 

 

Improved global action filter of the Controller

Sometimes you want to execute some processing logic before or after the execution of an action method, in ASP. net MVC2 provides an action filter that allows you to process the action methods of a specific controller. In fact, sometimes you want to perform similar processing on all actions, mvc3 allows you to add a filter to the globalfilters collection to create a global filter. For details, refer to the following resources:

  • Scott Guthrie's blog on the MVC 3 Preview
  • Filtering in ASP. NET MVC
New viewbag attributes

The Controller in MVC2 supports the viewdata attribute, and allows the data to be transmitted to the view template through the dictionary bound later. In mvc3, you can use viewbag to perform more easily. For example, for viewdata ["message"] = "text", you can use viewbag. Message = "text. You do not need to define any strongly-typed attributes through the class, because this is a dynamic attribute. Internally, the viewbag attribute is saved in the viewdata dictionary as a name-value pair. Note: In many prerelease versions, this attribute is called viewmodel.

New actionresult type

The following actionresult type is added or extended in mvc3.

  • . Return 404 http status to the client. httpnotfoundresult
  • . Based on a Boolean parameter, a temporary redirection (HTTP 302 Status Code) or persistent redirection (HTTP 301 Status Code) is returned. Combined with this improvement ,, three methods are provided to support persistent redirection:, and. these methods return a real object instance. RedirectresultcontrollerRedirectPermanentRedirectToRoutePermanentRedirectToActionPermanentPermanentRedirectResult
  • . Return the HTTP status code specified by the user. Httpstatuscoderesult
JavaScript and Ajax improvements

By default, in mvc3, Ajax and validation use unobtrusive Javascript methods that are not compelling. Unobtrusive does not insert Javascript in the line in HTML, which makes HTML more concise and less interfering. It also makes it easier to replace and customize JavaScript libraries. In mvc3, the Verification Assistant uses jquery by default. the validate plug-in is complete. If you want to use MVC2 behavior, you can. disable unobtrusive through configuration in config. For more information, refer to the following resources:

  • Basic Introduction to unobtrusive JavaScript on the Wikipedia site
  • Brad Wilson's unobtrusive JavaScript post
  • Brad Wilson's unobtrusive JavaScript Validation post
  • (Tutorial on the ASP. NET site) Creating a MVC 3 Application with razor and unobtrusive Javascript
  • MVC 3 release notes
Client verification is enabled by default.

In earlier versions of MVC, You need to explicitly call the HTML. enableclientvalidation method in the view to enable client verification. In mvc3, client verification is enabled by default. You can disable it in Web. config.

To verify the client, you still need to add jquery and jquery to the website. you can provide a reference to the validation library on your website, or use a Microsoft or Google CDN server.

Remote verification

ASP. NET 3 provides support for remote verification of the jquery validation plug-in through a new tag remoteattribute. This allows the client's verification database to automatically call a custom method defined on the server to complete the verification logic that can only be completed on the server.

In the following example, the remote tag specifies that the username field is verified by a method defined in userscontroller named usernameavailable.

Public class user
{
[Remote ("usernameavailable", "users")]
Public String username {Get; set ;}
}

 

The following code is defined in the Controller

Public class userscontroller
{
Public bool usernameavailable (string username)
{
If (myrepository. usernameexists (username ))
{
Return "false ";
}
Return "true ";
}
}

 

For more resources about remote attributes, see How to: implement remote validation in ASP. NET MVC

JSON binding support

ASP. NET mvc3 contains built-in JSON binding support, which allows the Action Method to receive JSON-encoded data and model it as an action parameter. This capability is often used in client template and data binding. The client template allows you to format and display one or more data using the client template. mvc3 allows you to simply connect the client template to the server action method and send and receive data using JSON, for more information, see Scott Guthrie's MVC 3 Preview blog post.

Improved dataannotations metadata tag for Model Verification

ASP. NET mvc3 supports dataannotations metadata tags, such as displayattribute.

Validationattribute class

The improved validationattribute class in. Net framework4 supports the new isvalid overload to provide more information about the current authentication context, such as what object is verified. This allows you to verify the current value based on other attributes of the model. For example, the new compareattribute allows you to compare the values of the two attributes of the model. In the following example, the comparepassword attribute must match the password field to pass verification.

Public class user
{
[Required]
Public String password {Get; set ;}
[Required, compare ("password")]
Public String comparepassword {Get; set ;}
}

 

Verification Interface

The ivalidatableobject interface allows you to perform model-level verification, and allows you to provide verification error information for the entire model status, or based on two attributes of the model. When the model is bound, mvc3 receives error messages from ivalidatableobject. When the built-in HTML assistant is used in the view, the affected fields are automatically identified or highlighted.

The iclientvalidatable interface allows ASP. net mvc to discover supported client validators at runtime. This interface is used to support integration of different verification frameworks.

For more information about the verification interface, see model validation improvements in Scott Guthrie's MVC 3 Preview blog post.

Dependency injection dependency injection Improvement

ASP. NET mvc3 provides better Di and IOC support, and supports di in the following areas:

  • Controller (registering and injecting controller factories, injecting controllers ).
  • View (registering and injecting view engines, injecting dependencies into view pages ).
  • Action filter (locating and injecting filters ).
  • Registering and injecting ).
  • Registering and injecting ).
  • Registering and injecting ).
  • Value provider (registering and injecting ).

Mvc3 supports the common service locator library and any di container that supports the iservicelocator interface of this library. It also supports the new idependencyresolver interface that is easy to integrate into the di framework.

For more information about Di, refer:

  • Brad Wilson's series of blog posts on service location
  • MVC 3 release notes
Other new features nuget Integration

ASP. NET mvc3 automatically installs and enables nuget. nuget is a free and open-source package manager that makes it easy to discover, install, and use. Net libraries in your project. It can work with all Visual Studio project types, including ASP. NET webform and MVC.

Nuget allows developers to maintain open-source projects, such as Moq projects and nhib.pdf, and register them on an online website.

For more information, see nuget documentation on the codeplex site.

Partial-page output Cache

ASP. net mvc supports full-page caching from version 1 and mvc3 also provides partial-page caching. This allows you to easily cache an output region or segment. For more information, see Scott Guthrie's blog post on the MVC 3 release candidate.Partial page output cachingSection, and in MVC 3 release notesChild Action output cachingSection.

Granularity Control in request verification

ASP. net mvc has a built-in request validation mechanism to automatically help handle cross-site attacks and HTML injection. In fact, sometimes you want to explicitly disable request verification. For example, you want to allow users to submit HTML content, for example, in the content management system, now you can add the allowhtml tag to the model or view model to disable request verification based on an attribute during binding. For more information, see:

  • In the. Scott Guthrie's blog post on the MVC 3 release candidateUnobtrusive JavaScript and validation
  • MVC 3 release notes
Extensible new project dialog box

In mvc3, you can add a project template, view engine, and unit test project framework to the new project dialog box.

Improvement of scaffolding

Scaffolding in mvc3 provides better support for primary keys. For example, the scaffold template does not add the primary key to the editing form.

By default, the created and edited scaffolding uses the HTML. editorfor assistant to replace the HTML. textboxfor assistant. This improvement supports metadata tags in the model when the view dialog box is added to generate a view.

New loads of HTML. labelfor and HTML. labelformodel

A new method overload is added for labelfor and labelformodel, allowing you to specify or override label text.

Controller support without session

In mvc3, you can specify whether the controller uses the session status, and whether the session is read/write or read-only.

New additionalmetadataattribute class

You can use the additionalmetadataattribute label to access the modelmetadata. additionalvalues dictionary for an attribute of the model. For example, if an attribute of the model only supports display by the Administrator, you can set it as follows:

Public class productviewmodel
{
[Additionalmetadata ("adminonly", true)]
Public String refundcode {Get; set ;}
}

 

When the product model is used to generate the metadata, the metadata will be displayed or used by any editing template, which allows you to interpret metadata information.

 

Source: http://www.asp.net/mvc/mvc3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.