What are the differences among mvc1, mvc2 and mvc3?

What are the differences among mvc1, mvc2 and mvc3?

mvc1 mode:

The view receives user input and passes the command to the controller
The controller processes the command and updates the model
After the model is updated, the view will be notified that it needs to be updated
The view is updated and displayed to the user

MVC2 mode:

In MVC1, the model can notify the view, and then the view can be updated. This is very common in Windows programs, such as the frame-document-view architecture of MFC. If the document changes, it will actively notify the view to update.

However, in the web, after the Java bean as the model is updated, the JSP as the view cannot be called (and there are usually many views and you need to choose one), so the MVC2 mode is changed:

The view receives user input and passes the command to the controller
The controller processes the command and updates the model
After the model is updated, the controller will select a view and forward it to the jsp, with the model as a request parameter. The view gets the model and displays it.
A complete analysis of the new features of ASP.NET MVC 3

ASP.NET MVC 3 adds a number of features to ASP.NET MVC 1 and 2, making the code more streamlined and deeply extensible. This article provides descriptions of many of the new features included in this release.

Razor View Engine
ASP.NET MVC3 brings a new view engine called Razor, which provides the following advantages:

Razor now provides some new features:

Razor also includes new HTML Helpers, such as:

Support for multiple view engines <br /> In ASP.NET MVC3, the Add View dialog allows you to select the view engine you want. In the New Project dialog, you can specify the default view engine for the project. You can choose WebForm, Razor, or an open source view engine such as Spark, NHaml, or NDjango.

Controller improvements
Global Action Filters <br /> Sometimes you want to be able to execute some processing logic before or after an Action method is executed. In ASP.NET MVC2, Action filters are provided to allow processing of Action methods of specific controllers. In fact, sometimes you want to perform similar processing on all Actions. MVC3 allows you to add filters to the GlobalFilters collection to create global filters.

New ViewBag Properties
Controllers in MVC2 support ViewData properties, allowing data to be passed to view templates through post-bound dictionaries. In MVC3, you can do this more simply through ViewBag. For example, for ViewData["Message"] = "text", you can do it through ViewBag.Message = "text". You do not need to define any strongly typed properties through the class, because this is a dynamic property. Internally, ViewBag properties are stored in the ViewData dictionary as name-value pairs. Note that in many pre-release versions, this property was called ViewModel.

New ActionResult Types <br /> The following ActionResult types are new or extended in MVC3.

JavaScript and Ajax Improvements <br /> By default, in MVC3, Ajax and validation use unobtrusive JavaScript. Unobtrusive does not insert inline JavaScript into the HTML, which makes the HTML more concise and less intrusive, and also makes it easier to replace and customize JavaScript libraries. In MVC3, validation helpers are done by default using the jQuery.Validate plugin. If you want to use the MVC2 behavior, you can turn off unobtrusive in the web.config configuration.

Client validation is enabled by default In earlier versions of MVC, you needed to explicitly call the Html.EnableClientValidation method in the view to enable client validation. In MVC3, this is no longer necessary because client validation is enabled by default. It can be turned off in web.config.

In order for client-side validation to work, you still need to include references to jQuery and the jQuery.Validation library in your website, either by serving them on your own site or using a CDN server from Microsoft or Google.

Remote Verification
ASP.NET 3 provides support for remote validation of the jQuery Validation plugin through a new tag RemoteAttribute. This allows the client-side validation library to automatically call a custom method you define on the server to complete the validation logic that can only be completed on the server.

In the following example, the Remote tag specifies that the username field be validated through a method named UserNameAvailable defined in UsersController.

Razor's syntax is simple and clear, requiring minimal typing.
Razor is easy to learn, and its syntax is similar to C# and VB
Visual Studio provides smart tips and syntax coloring for Razor
Razor views can be tested without running the application or starting a web server
@model is used to specify the Model type passed to the view
The @* * comment syntax can be used to set default items, such as layout, for the entire site at once.
The Html.Raw method provides output without HTML encoding to support sharing code between multiple views (_viewstart.cshtml or _viewstart.vbhtml)
Chart. Generate a chart
WebGrid, generates data tables with full paging and sorting support
Crypto, using hashing algorithms to create hashed and salted passwords
WebImage, generate images
WebMail, Sending Email
HttpNotFoundResult. Returns a 404 HTTP status to the client.
RedirectResult. Based on a Boolean parameter, returns a temporary redirect (HTTP 302 status code) or a permanent redirect (HTTP 301 status code). In conjunction with this improvement, Controller provides three methods to support persistent redirects: RedirectPermanent, RedirectToRoutePermanent, and RedirectToActionPermanent. These methods return a RedirectResult object instance with the Permanent property set to true.
HttpStatusCodeResult. Returns the HTTP status code specified by the user.

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

The following code is defined in the controller

  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 resources on the Remote attribute, refer to 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 includes built-in JSON binding support, allowing action methods to receive JSON-encoded data and model it as action parameters. This capability is often used in client-side templates and data binding. Client-side templates allow you to format and display one or more data through client-side templates. MVC3 allows you to simply connect client-side templates and server-side action methods to send and receive data through JSON. For more information, refer to Scott Guthrie's MVC 3 Preview blog post.

ValidationAttribute Class <br /> The ValidationAttribute class has been improved in .NET Framework 4 to support new IsValid overloads that provide more information about the current validation context, such as what object is being validated. This allows you to validate the current value based on other properties of the Model. For example, the new CompareAttribute allows you to compare the values ​​of two properties of the Model. In the following example, the ComparePassword attribute must match the Password field to pass validation.

Improvements to Model Validation
DataAnnotations metadata tags
ASP.NET MVC3 supports DataAnnotations metadata tags, such as DisplayAttribute.

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

Verification interface

The IValidatableObject interface allows you to perform model-level validation and allows you to provide validation error messages for the entire model state, or based on two properties of the model. When the model is bound, MVC3 receives error messages from IValidatableObject and when using the built-in HTML helpers in the view, the affected fields will be automatically identified or highlighted.

The IClientValidatable interface allows ASP.NET MVC to discover supported client-side validators at runtime. This interface is used to support integration with different validation frameworks.

For more information about validation interfaces, see the Model Validation Improvements section in Scott Guthrie's MVC 3 Preview blog post.

Dependency Injection Improvements
ASP.NET MVC3 provides better DI and IoC support, supporting DI in the following places:

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 for easy integration into the DI framework.

Other new features

NuGet Integration
ASP.NET MVC3 automatically installs and enables NuGet, a free and open source package manager that makes it easy to discover, install, and use .NET libraries in your projects. It works with all Visual Studio project types, including ASP.NET WebForm and MVC.

NuGet allows developers maintaining open source projects, for example, like Moq Project, NHibernate, etc., to register them to an online website.

For more information, see the NuGet documentation on the CodePlex site.

Output caching for partial pages
ASP.NET MVC has supported full page caching since version 1, and MVC 3 also provides partial page caching. This allows you to easily cache a region or fragment of output, see the Partial Page Output Caching section in Scott Guthrie's blog post on the MVC 3 release candidate for more information, and the Child Action Output Caching section in the MVC 3 Release Notes.

Granular control in request validation
ASP.NET MVC has a built-in request validation mechanism to automatically help handle cross-site attacks and HTML injections. In fact, sometimes you want to explicitly turn off request validation, for example, you want to allow users to submit HTML content, such as in a content management system. Now you can add the AllowHtml tag to the Model or the view's Model to support turning off request validation based on a property when binding. For more information, refer to:

Expandable New Project dialog

In MVC3, you can add project templates, view engines, and unit test project frameworks to the New Project dialog.

Scaffolding improvements
The scaffolding in MVC3 provides better support for primary keys. For example, the scaffolding template will not add the primary key to the edit form.

By default, create and edit scaffolds now use the Html.EditorFor helper instead of the Html.TextBoxFor helper. This improvement supports metadata tags in the model when generating a view in the Add View dialog.

New Overloads for Html.LabelFor and Html.LabelForModel PDF has added new method overloads for LabelFor and LabelForModel that allow specifying or overriding the Label text.

No Session Controller Support
In MVC3, you can specify whether the controller uses the Session state, and whether the Session is read-write or read-only.

The new AdditionalMetadataAttributePDF class allows access to the ModelMetadata.AdditionalValues ​​dictionary for a property of a Model through the AdditionalMetadataAttribute tag. For example, if a property of a model only supports administrator display, 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 filters (locating and injecting filters).
Model binder (registering and injecting).
Model validation providers (registering and injecting).
Model metadata provider (registering and injecting).
Value provider IC trading network (registering and injecting).
Brad Wilson's series of blog posts on Service Location
MVC 3 Release Notes
Unobtrusive JavaScript and Validation in Scott Guthrie's blog post on the MVC 3 release candidate.
MVC 3 Release Notes

<<:  Example analysis: Performance optimization of "Qichu Encyclopedia"

>>:  ASP.NET MVC/C# development tips record.

Recommend

They actually live on the seabed where there is no sunlight!

The mid-ocean ridge is 2000~3000m below the sea s...

Xu Guanhua: Zhou Guangzhao's scientific light will always shine

This article was published in the Bulletin of the...

Apple urgently releases iOS 14.7.1 official version to fix two major bugs

[[413605]] The official version of iOS 14.7.1 has...

Young people, are your health-preserving methods reliable?

On Douban, a group called "We Love Health Pr...