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 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