Mvc1 mode:
View receives user input and transmits the command to the Controller
Controller Processing Command, update Model
After the model is updated, the view needs to be updated.
View updated and displayed to users
MVC2 mode:
In mvc1, the model can notify the view and the view can be updated. This is common in Windows programs, such as the frame-document-view architecture of MFC. If the document changes, he will automatically notify the view to update
However, in the Web, after the Java Bean updated as the model, it cannot be called as the view JSP (and there are usually many views and one needs to be selected). Therefore, it is changed to the MVC2 mode:
View receives user input and transmits the command to the Controller
Controller Processing Command, update Model
After the model is updated, the Controller selects a view and sends the view to the JSP, with the model placed in the request parameter.
This view gets the model and then displays
Full Analysis of ASP. net mvc 3 new features
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.
-
- 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
- @ 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, which uses the hash algorithm to create the hash and salt-adding passwords
- 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.
- 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 properties, see How to: implement remote validation in ASP. net mvc http://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.
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.
Improvement of Model Verification
Dataannotations metadata tag
ASP. NET mvc3 supports dataannotations metadata tags, such as displayattribute.
- 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:
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.
For HTML. labelfor and HTML. labelformodel PDF
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 additionalmetadataattributepdf 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:
-
- 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 provides the 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