Learn more about the differences between ASP.NET MVC and WebForm

零下一度
Release: 2017-05-20 13:29:38
Original
2541 people have browsed it

Talking about the difference between ASP.NET MVC and WebForm

Using the ASP.NET MVC framework to create a default project, the first intuitive feeling is that the addresses are all Rewritten. After a slight analysis of the source code and configuration files, it is not difficult to see that MVC uses httpModules to intercept address requests, specifically using the System.Web.Routing class library (in MVC2, I have forgotten how to use MVC1.) and this part of the class library is packaged In .NET Framework3.5 SP1, it is natural that MVC2 requires SP1 support. The System.Web.Routing class library provided by SP1 can easily intercept address requests and is also excellent in encoding processing. The UrlRoutingModule class intercepts requests. Before that, during Application_Start, an interception setting will be given to the global object of RouteTable. This setting is saved using the RouteCollection object, and MVC extends this class - RouteCollectionExtensions. These can be ignored. Next, when the user accesses the page, the UrlRoutingModule class intercepts the request and checks whether it meets the rules in the RouteTable. If it meets the rules, MvcHandler will be called. This call is registered in the httpHandlers configuration node, provided that the address matches "* .mvc” rules. The ProcessRequest method of MvcHandler will call the Controller to execute. In fact, the entire process is a black box and users cannot feel it. After a method is executed in the Controller, the result is returned and then the specific aspx page is entered.

After analyzing the working project of MVC, you can compare its difference with WebForm. We know that MVC mode business is placed in the Controller for execution, and the aspx page is only responsible for display. Then the actual execution time of the business in MVC is advanced to HttpMolde, and the WebForm request is only executed in the httpHandler container. In other words, the separation of Controller and View in MVC is achieved by using the ASP.Net request pipeline. In this way, the logical level of the code is undoubtedly achieved without affecting the efficiency (one request, while Response.Redirect is a second request). of separation.

Learn more about the differences between ASP.NET MVC and WebForm

The advantages of MVC work are obvious, and it is more conducive to understanding the layered logic and grasping the layering of the code. The process between the Controller and the aspx page has been isolated by the framework. As for the process of calling the Controller or View page and Model, you still need to master it yourself. ASP.NET's MVC framework implements separate management of Controller code.

Looking at the WebForm development model, it is only executed in the HttpHandler container and layered. It lacks support in large aspects and can only rely on logical separation. It’s not that they cannot be separated, but that they have certain limitations. HttpHandler's interception is related to accessing the suffix name. When a page is requested, it is a Handler, and the WebForm model realizes the separation of display and logic, and only WinForm's eventdriver is available. Obviously, the event must be registered to the page, such as Button1_Click code. Before Button1_Click is executed, the Page_Load method will be executed.

The display code is written into the Page_Load method, which will result in the need to write additional waste code, such as if (!Page.IsPostBack). The part that needs to be displayed after Button1_Click is executed is more difficult to handle. Writing another method must also be called in Button1_Click. The alternative solution is to use Response.Redirect, process the logic in an aspx page, and jump to another displayed page after processing. The disadvantage of this is that it is difficult to share data between the two pages, and the jump is implemented by marking 302, so there is one more request. In addition, through processing methods such as Server.Execute, Server.Transfer or Context.RewritePath, the conversion of the two pages is completed on the server side and data can be shared. It can be said that the processing method is similar to that of the MVC framework. The disadvantage is that it needs to be done manually. Configure these redirection properties.

It can be seen from the above analysis that the MVC framework has strong advantages, and WebForm is not without its merits. It is easier to develop in simple applications. WebForm can also implement the same layering method as MVC, but you need to write more code when processing. In my opinion, the biggest problem encountered when using WebForm to develop hierarchical development is the transfer of data between pages, and mastering the application skills of using server-side jumps in WebForm (Server.Execute, Server.Transfer or Context. RewritePath) can solve the data transmission problem. Compared with ASP.NET MVC and WebForm, WebForm is easier to understand and does not produce complicated configurations. It is also a very good choice.

【Related recommendations】

1. What is ASP.NET MVC? Summary of ASP.NET MVC

2. Detailed introduction to ASP.NET MVC--controller

3. Detailed introduction to ASP.NET MVC--View

4. Detailed introduction to ASP.NET MVC--Routing

5 . Code example for developing WeChat custom menu editing tool through asp.net mvc

The above is the detailed content of Learn more about the differences between ASP.NET MVC and WebForm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template