Full Analysis of ASP. net mvc 3 new features

Source: Internet
Author: User
      ASP. NET mvc3 adds a large number of features based on ASP. net mvc 1 and 2, enabling CodeSimplified and scalable. This article ArticleIt provides a description of many new features included in this release, which are divided into the following parts:
    1. Public ClassProductviewmodel{
    2. [Additionalmetadata ("Adminonly",True)]
    3. Public StringRefundcode {Get;Set;}
    4. }
      1. Experience razor features in ASP. net mvc 3
      2. Upgrade ASP. NET MVC2 project to MVC 3 RC
      3. Awesome chart in ASP. net mvc 3 beta
      • 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.
      • The razor view is not allowed.ProgramOr you can start the Web server for testing.
      • @ 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)
      • Chart. Generate chart
      • Webgrid: generates data tables and supports complete paging and sorting.
      • Crypto, use HashAlgorithmTo create a hash and salt-adding password.
      • Webimage to generate images
      • Webmail: send an email
      • Httpnotfoundresult. Return the 404 http status to the client.
      • Redirectresult. 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 ,, the Controller provides three methods to support persistent redirection: redirectpermanent, redirecttoroutepermanent, and redirecttoactionpermanent. these methods return an instance with the permanent attribute true redirectresult.
      • Httpstatuscoderesult. The HTTP status code specified by the user is returned.
      • 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 ).
      • Brad Wilson's series of blog posts on service location
      • MVC 3 release notes
      • Scott Guthrie's blog post on the MVC 3 Release Candidate unobtrusive JavaScript and validation section.
      • MVC 3 release notes
  • 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.

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

    Razor now provides some new features:

    Razor also contains the new HTML helper, for example:

    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.

    Controller Improvement
    Global action Filter
    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 set to create a global filter.

    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.

    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.

    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.

  1. Public ClassUser
  2. {
  3. [Remote ("Usernameavailable","Users")]
  4. Public StringUsername {Get;Set;}
  5. }

The following code is defined in the Controller

  1. Public ClassUserscontroller
  2. {
  3. Public BoolUsernameavailable (StringUsername)
  4. {
  5. If(Myrepository. usernameexists (username ))
  6. {
  7. Return "False";
  8. }
  9. Return "True";
  10. }
  11. }

For more resources about remote attributes, see How to: implement remote validation in ASP. NET MVCHttp://msdn.microsoft.com/en-us/library/gg508808 (vs.98). aspx

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.

Improvement of Model Verification
Dataannotations metadata tag
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.

  1. Public ClassUser
  2. {
  3. [Required]
  4. Public StringPassword {Get;Set;}
  5. [Required, compare ("Password")]
  6. Public StringComparepassword {Get;Set;}
  7. }

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:

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:

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 the section "partial page output caching" in Scott Guthrie's blog post on the MVC 3 release candidate, there is also the Child Action output caching section in MVC 3 release notes.

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:

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:

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.

Original article:Http://www.cnblogs.com/haogj/archive/2011/01/15/1936473.html

[Edit recommendations]

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.