asp.net-mvc ASP.NET Webforms 与 ASP.NET MVC 的优缺点,一些要点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7674102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:30:33  来源:igfitidea点击:

Advantages and Disadvantages of ASP.NET Webforms Vs ASP.NET MVC, some points

asp.net-mvcasp.net-mvc-3asp.net-mvc-2

提问by Luis Valencia

Possible Duplicate:
Biggest advantage to using ASP.Net MVC vs web forms

可能的重复:
使用 ASP.Net MVC 与 Web 表单的最大优势

I was reading this document

我正在阅读这份文件

http://msdn.microsoft.com/en-us/library/dd381412(VS.98).aspx

Specifically the advantages and disadvantages:

具体优缺点:

Advantages of an MVC-Based Web Application

基于 MVC 的 Web 应用程序的优点

The ASP.NET MVC framework offers the following advantages:

ASP.NET MVC 框架具有以下优点:

It makes it easier to manage complexity by dividing an application into the model, the view, and the controller.

通过将应用程序划分为模型、视图和控制器,可以更轻松地管理复杂性。

It does not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application.

它不使用视图状态或基于服务器的表单。这使得 MVC 框架非常适合希望完全控制应用程序行为的开发人员。

It uses a Front Controller pattern that processes Web application requests through a single controller. This enables you to design an application that supports a rich routing infrastructure. For more information, see Front Controller.

它使用前端控制器模式,通过单个控制器处理 Web 应用程序请求。这使您能够设计支持丰富路由基础结构的应用程序。有关更多信息,请参阅前端控制器。

It provides better support for test-driven development (TDD).

它为测试驱动开发 (TDD) 提供了更好的支持。

It works well for Web applications that are supported by large teams of developers and for Web designers who need a high degree of control over the application behavior.

它适用于由大型开发人员团队支持的 Web 应用程序以及需要高度控制应用程序行为的 Web 设计人员。

Advantages of a Web Forms-Based Web Application

基于 Web 表单的 Web 应用程序的优点

The Web Forms-based framework offers the following advantages:

基于 Web 窗体的框架具有以下优点:

It supports an event model that preserves state over HTTP, which benefits line-of-business Web application development. The Web Forms-based application provides dozens of events that are supported in hundreds of server controls.

它支持通过 HTTP 保留状态的事件模型,这有利于业务线 Web 应用程序开发。基于 Web 窗体的应用程序提供了数百个服务器控件支持的数十个事件。

It uses a Page Controller pattern that adds functionality to individual pages. For more information, see Page Controller.

它使用页面控制器模式为单个页面添加功能。有关更多信息,请参阅页面控制器。

It uses view state on server-based forms, which can make managing state information easier.

它在基于服务器的表单上使用视图状态,这可以更轻松地管理状态信息。

It works well for small teams of Web developers and designers who want to take advantage of the large number of components available for rapid application development.

它适用于希望利用大量可用组件进行快速应用程序开发的小型 Web 开发人员和设计人员团队。

In general, it is less complex for application development, because the components (the Page class, controls, and so on) are tightly integrated and usually require less code than the MVC model.

一般来说,它对于应用程序开发来说不太复杂,因为组件(Page 类、控件等)是紧密集成的,并且通常比 MVC 模型需要更少的代码。



Because I am a newbie on MVC but not on webforms, I have the following questions regarding this advantages and disadvantages:?

因为我是 MVC 的新手,但不是 webforms 的新手,所以我有以下关于这个优点和缺点的问题:?

  1. MVC has no viewstate? So how can I save data in the current page? This seems a very big disadvantage.

  2. Webform says that provide an extensive event model with lots of controls, Doesnt MVC support controls and events as a normal webform?

  1. MVC 没有视图状态?那么如何在当前页面保存数据呢?这似乎是一个非常大的缺点。

  2. Webform 说提供了一个包含大量控件的扩展事件模型,MVC 不支持控件和事件作为普通 Webform 吗?

Lets suppose I am an expert on both? why would I choose one or the other? So far the only good thing about MVC in my opinion is testability, becase Rapid Application Development in webforms win in so many ways to MVC.(Just my opinion)

让我们假设我是这两个方面的专家?为什么我会选择其中之一?到目前为止,我认为 MVC 唯一的好处是可测试性,因为 webforms 中的快速应用程序开发在很多方面都胜过 MVC。(只是我的意见)

Now talking about the RazonEngine, it seems like going back to the old ASP, you cant use server side controls? how is this an advantage for me?

现在说到RazonEngine,好像回到了旧的ASP,不能用服务器端控件了吗?这对我有什么好处?

回答by Darin Dimitrov

1) MVC has no viewstate? So how can I save data in the current page? This seems a very big disadvantage.

1)MVC没有viewstate?那么如何在当前页面保存数据呢?这似乎是一个非常大的缺点。

Why do you want to save things in the page? That's what databases are supposed to do. Save data. In the page you simply use a hidden field allowing you to refetch the data from wherever it was initially stored. And for data that can change you have form input fields, so it will be there.

为什么要在页面中保存东西?这就是数据库应该做的事情。保存数据。在页面中,您只需使用一个隐藏字段,允许您从最初存储的任何位置重新获取数据。对于可以更改的数据,您有表单输入字段,所以它会在那里。

2) Webform says that provide an extensive event model with lots of controls, Doesnt MVC support controls and events as a normal webform?

2) Webform 说提供了一个包含大量控件的广泛事件模型,MVC 不支持控件和事件作为普通 Webform 吗?

No, MVC doesn't support controls and events. It supports models, controllers and views.

不,MVC 不支持控件和事件。它支持模型、控制器和视图。

Lets suppose I am an expert on both? why would I choose one or the other? So far the only good thing about MVC in my opinion is testability, becase Rapid Application Development in webforms win in so many ways to MVC.

让我们假设我是这两个方面的专家?为什么我会选择其中之一?到目前为止,我认为 MVC 唯一的好处是可测试性,因为 webforms 中的快速应用程序开发在很多方面都胜过 MVC。

That's a subjective question for which there is no objective answer. The answer I can give you is: it will depend on the context. I agree that RAD wins in WebForms, but what you have to remember is that it is not the development of the application that costs most. It's its support and maintenance. So if your application is written rapidly you will ship it quickly indeed but with the time if you don't properly architect it with extensibility in mind this quick application development turns slowly into a nightmare, until the day comes where you simply have to rewrite it because it costs you so much to continue to support it.

这是一个主观的问题,没有客观的答案。我可以给你的答案是:这取决于上下文。我同意 RAD 在 WebForms 中获胜,但您必须记住的是,成本最高的并不是应用程序的开发。这是它的支持和维护。因此,如果您的应用程序编写得很快,您确实会很快发布它,但是随着时间的推移,如果您没有正确地构建它并考虑到可扩展性,这种快速的应用程序开发将慢慢变成一场噩梦,直到​​有一天您只需要重写它因为继续支持它需要花费很多。

Now talking about the RazonEngine, it seems like going back to the old ASP, you cant use server side controls? how is this an advantage for me?

现在说到RazonEngine,好像回到了旧的ASP,不能用服务器端控件了吗?这对我有什么好处?

You have total control of the markup, your pages are no longer polluted with useless ViewStates, and at last your markup validates. If you want to reuse some view portions in ASP.NET MVC you could use partial views.

您可以完全控制标记,您的页面不再被无用的 ViewStates 污染,最后您的标记得到验证。如果您想在 ASP.NET MVC 中重用某些视图部分,您可以使用部分视图。

Here's what I can give you as an advice. As you are already an expert in WebForms, that will be a great advantage for you when learning ASP.NET MVC. So learn it and by learning it you will probably start to see by yourself the different advantages and be able to answer yourself the question which one of the two suits you better.

这是我可以给你的建议。由于您已经是 WebForms 的专家,这对您在学习 ASP.NET MVC 时将是一个很大的优势。所以学习它,通过学习它,你可能会开始自己看到不同的优势,并能够回答自己这两者中哪一个更适合你的问题。

回答by Steve Morgan

ASP.NET MVC applications hand you back control over the way your application works and it conforms to the 'native' model of HTTP/HTML.

ASP.NET MVC 应用程序将您的应用程序的工作方式交还给您,它符合 HTTP/HTML 的“本机”模型。

WebForms is an abstraction of web application programming; envisaged to ease the transition for the Visual Basic programmers who were the primary target for .NET when it was launched. The trouble is, it's a 'leaky abstraction'. It uses 'tricks' under the cover to make it appear to operate in a way that is simple for those used to VB's event-based model, such as viewstate and postbacks. The trouble with this (and all those 'leaky abstractions') is that while the basic stuff is easy, once you try to do something that doesn't fit within the abstraction, it gets dramatically more complicated.

WebForms 是 Web 应用程序编程的抽象;旨在简化 Visual Basic 程序员的过渡,他们是 .NET 推出时的主要目标。问题是,这是一个“泄漏的抽象”。它在幕后使用“技巧”,使其看起来以一种对那些习惯于 VB 的基于事件的模型(例如视图状态和回发)的人来说很简单的方式操作。这个(以及所有那些“有漏洞的抽象”)的问题在于,虽然基本的东西很简单,但一旦你尝试做一些不适合抽象的事情,它就会变得更加复杂。

If your applications are trivial and the WebForms abstraction consistently works for you, feel free to stick with it. Microsoft have been considerate enough to continue to support it.

如果您的应用程序是微不足道的,并且 WebForms 抽象始终适合您,请随时坚持使用它。微软已经足够周到,继续支持它。

But if your applications are at all sophisticated or complex (as more and more are these days), I'd suggest looking at MVC with an open mind.

但是,如果您的应用程序非常复杂或复杂(现在越来越多),我建议您以开放的心态看待 MVC。

As a test, take a web developer from a non-Microsoft background and let them loose on both webforms and MVC and see which they're more productive with.

作为测试,请一位非 Microsoft 背景的 Web 开发人员让他们在 webforms 和 MVC 上放松,看看他们使用哪个更有效率。

回答by Daryl Teo

ViewState is a curse, a crutch for people who don't understand that HTTP is a stateless protocol, and try to hammer in a hacky solution for persisting state across requests, leading to a massive fail.

ViewState 是一个诅咒,对于那些不了解 HTTP 是无状态协议的人来说是一个拐杖,并试图敲定一个跨请求持久化状态的黑客解决方案,导致大规模失败。

  • Concurrency between multiple clients goes out the window.
  • It is stored as a massive chunk of data in a hidden field leading to increased page load sizes.
  • It is wholly dependent on POST methods, so it breaks navigation in HTML.
  • 多个客户端之间的并发性消失了。
  • 它作为大量数据存储在隐藏字段中,导致页面加载大小增加。
  • 它完全依赖于 POST 方法,因此它破坏了 HTML 中的导航。

Simply put, it is not user friendly at all.

简而言之,它根本不是用户友好的。

And the markup on those controls are horrible too.

这些控件上的标记也很糟糕。



Regarding UserControls: you can use Partials to emulate some of the functionality of Controls.

关于 UserControls:您可以使用 Partials 来模拟 Controls 的一些功能。

Events: you model Events through Actions in controllers.

事件:您可以通过控制器中的动作对事件进行建模。



Flame War commence!

火焰War开始!

回答by Artem Koshelev

Rapid Application Development in webforms win in so many ways to MVC

webforms 中的快速应用程序开发在很多方面都胜过 MVC

Yes. Use WebForms if you need RAD, use MVC if you rely more on JavaScript, CSS and HTML.

是的。如果您需要 RAD,请使用 WebForms,如果您更依赖 JavaScript、CSS 和 HTML,请使用 MVC。

And use search: https://stackoverflow.com/search?q=webforms+vs+mvc

并使用搜索:https: //stackoverflow.com/search?q=webforms+vs+mvc