The advantages and disadvantages of MVC and WebForm and the use of MVC

Source: Internet
Author: User

There are countless comments on the pros and cons of MVC and WebForm on the web, but it's not a big deal to get out of our project. With our revision, WebForm's advantage has the following points:

One, can use < #include >,css, HTML and JS can be used to achieve cross-page and even Kua project reuse, MVC did not find such a function;

Second, the user control can be precisely called properties, fields, functions and can get the corresponding return value, MVC did not find such a function;

Third, it is convenient to apply the public or protective field property functions to the ASPX page, and MVC cannot directly invoke the corresponding field properties in the control.

In view of the above WebForm or MVC indeed point, we in the revision shop office building followed the following several norms to compensate:

One, using the partial view to realize the reuse of HTML;

Second, a reasonable encapsulation of the attribute field functions used in the controller, such as the integration of the original variables into a large class as a strongly typed view of the object passed to the corresponding view, the implementation of the view is convenient to use. In this case, the division of the class needs to be as high as possible to put together the correlation, priority to use the combination of classes rather than inheritance, if you need to put the field in the View object is a large composite class, such as broker entities or listing entities, it is recommended to store separately in ViewBag. The purpose of these practices is more convenient to use these variables on the one hand, reducing the granularity of passing parameters when invoking a local view or function in a view, and improving the reusability of these partial views or functions, such as the function of arguments with broker entities. We can use it in a commercial office, can also be used in the ordinary broker Details page, the two-view object does not achieve this parameter;

Third, put code reuse units on objects and functions, in principle, do not create in the controller does not directly accountable to the browser function, but placed at the bottom to facilitate reuse.

After following some of these specifications, there are some advantages to finding MVC:

First, the excellent razor engine makes the use of C # code in the view more convenient and smooth;

Two, a single view can correspond to multiple controllers, improve the reuse of code;

Third, objects stored in the ViewBag or ViewData of the main view can be used in the detail views (views rendered in renderpartial mode only) or master pages;

Four, you can reuse the normal HTML code in the view in a functional way into the detail view and you can pass an object to the detail view;

Five, the demarcation between the view and the controller is clearer, and the reuse of the partial view can be called directly in the view, while in WebForm the user control is not placed in the background code because of the need to pass the parameter binding to achieve higher reuse.

In addition to the similarities and differences above, there are also objects in WebForm and MVC that have the same name, but the type is not a type anymore, such as:

1         //2         //Summary:3         //gets the System.Web.HttpRequestBase object for the current HTTP request. 4         //5         //return Result:6         //The request object. 7          PublicHttprequestbase Request {Get; }8         //9         //Summary:Ten         //gets the System.Web.HttpResponseBase object for the current HTTP response.  One         // A         //return Result: -         //The response object.  -          PublicHttpresponsebase Response {Get; }
http objects in MVC
1         //2         //Summary:3         //gets the System.Web.HttpRequest object for the requested page. 4         //5         //return Result:6         //The System.Web.HttpRequest currently associated with the page. 7         //8         //Exception:9         //System.Web.HttpException:Ten         //occurs when the System.Web.HttpRequest object is not available.  One[Browsable (false)] A [DesignerSerializationVisibility (Designerserializationvisibility.hidden)] -          PublicHttpRequest Request {Get; } -         // the         //Summary: -         //gets the System.Web.HttpResponse object associated with the System.Web.UI.Page object. This object enables you to send HTTP response data to the client and contains information about the response.  -         // -         //return Result: +         //The System.Web.HttpResponse currently associated with the page.  -         // +         //Exception: A         //System.Web.HttpException: at         //The System.Web.HttpResponse object is not available.  -[Browsable (false)] - [DesignerSerializationVisibility (Designerserializationvisibility.hidden)] -          PublicHttpResponse Response {Get; }
HTTP objects in the WebForm

Because of the different types, some features in MVC are no longer valid, such as the Response.End () page that is not recognized in the controller will continue to run the following code, which can be replaced with return null. In addition, some playful property functions such as request.isajaxrequest () are added, which can be used to determine whether or not to access asynchronously. However, after all, the type of the corresponding object still share a base class, and most of their property function usages remain the same. At present, the new usage of the corresponding object is still very limited, it needs to be summed up in the future work in order to maximize the advantages of the framework.

As far as the revision is concerned, we are not doing well enough, but we can still find that in MVC we use less code and more places to reuse. In fact, whether it is MVC or webform as long as the use can be reasonable to achieve high efficiency and maintainability, to improve the maintainability and performance of the code needs us to customize and improve the unified standards, but also to improve our personal level.

The advantages and disadvantages of MVC and WebForm and the use of MVC

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.