ASP。净MVC > ASP。净WebForms,为什么?

时间:2021-03-26 03:21:38

I've now completed my first web application using ASP.NET MVC and overall, I still am not grasping why this is getting all the praise and glory. Maybe I'm being stubborn. I know that what makes MVC great is that is forces separation between the presentation layer and the business object or data layer along with its stateless operation. I also know that when you are working with the view, it appears that the code is less readable (see my example below).

现在我已经完成了我的第一个使用ASP的web应用程序。NET MVC和整体来说,我还是不明白为什么这得到了所有的赞扬和荣耀。也许我是固执。我知道MVC之所以伟大,是因为它将表示层与业务对象或数据层之间的分离与无状态操作分离开来。我还知道,当您使用视图时,代码的可读性似乎较差(参见下面的示例)。

So I guess my question is... If the separation is the concern, why not just separate.

所以我的问题是……如果要考虑分离,为什么不直接分离呢?

Web Forms View Code:

Web表单视图代码:

//UI
<h2><asp:Label ID="lblPageHeader" runat="server" /></h2>

Web Forms Code Behind:

Web表单的代码:

//CODE BEHIND
this.lblPageHeader.Text = myObject.Header;

MVC Code View:

MVC代码视图:

//UI
<h2><%= Html.Encode(Model.PageTitle) %></h2>

MVC Controller Code:

MVC控制器代码:

index = new Index
{
    PageText = sb.ToString(),
    PageTitle = "WELCOME"
};
return View(index);

Again, I might be being stubborn, but one of the things that I really liked in WebForms is the ease in setting object properties like DataSources and Text values. It seems like this is completely different in MVC and less readable which makes me wonder about long term maintenance.

同样,我可能有点固执,但是WebForms中我最喜欢的东西之一就是设置对象属性,比如数据源和文本值。这在MVC中似乎是完全不同的,可读性更差,这让我对长期维护产生了疑问。

EDIT After looking into the typed models, I think the readability of the code is substantially improved.

在查看了类型化模型之后,我认为代码的可读性得到了很大的提高。

9 个解决方案

#1


14  

ASP.NET MVC Best Practices, Tips and Tricks

ASP。NET MVC的最佳实践、技巧和技巧

I would write:

我想写:

<h2><%= Html.Encode(Model.Title) %></h2>

(it's possible with a help of typed views)

(可以借助类型化视图)

instead of

而不是

<h2><%= Html.Encode((MyApp.MyObject)ViewData["PageObject"].Header) %></h2>

I think it's all about how you use it. If you're more happy with classic ASP.NET, than maybe it will be a better idea to stick with it. Moreover, you could also take good stuff from ASP.NET MVC world (like UI and Logic separation) and bring it to the classic ASP.NET.

我认为关键在于你如何使用它。如果你对经典的ASP更满意的话。NET,也许坚持它会是一个更好的主意。此外,您还可以从ASP中获取好的内容。NET MVC世界(像UI和逻辑分离),并将它带到经典的ASP.NET中。

#2


10  

What's great about ASP.NET MVC is that is doesn't try to hide how HTTP works. To fully understand ASP.NET MVC you need to understand the technologies of the web.

有什么伟大的ASP。NET MVC并没有试图隐藏HTTP是如何工作的。充分了解ASP。您需要了解web的技术。

While webforms are adequate as long as you work to their strengths, they're ultimately a very leaky abstraction when you don't. While the drawbacks of viewstate have been well discussed by this point I think it's the extremely unwise attempt to mimic the behaviour of Winforms that is the underlying flaw - viewstate is merely a product of that.

虽然只要您发挥它们的长处,web表单就足够了,但如果不这样做,它们最终将是一个非常漏洞百出的抽象。虽然viewstate的缺点已经在这一点上得到了很好的讨论,但我认为,模仿Winforms的行为是潜在的缺陷——viewstate仅仅是这个缺陷的产物。

The web controls which ship with ASP.NET also leave a (hell of a) lot to be desired as anyone who has tried to build an accessible website can attest to. The web controls show a total lack of understanding for how frontend development is done, and frankly are a disgrace.

web控制使用ASP发送哪些内容。NET也有很多值得期待的地方,任何尝试建立一个可访问网站的人都可以证明这一点。web控件显示完全不了解前端开发是如何完成的,坦率地说,这是一种耻辱。

With ASP.NET MVC all that nonsense is done away with. You're not shielded from HTTP, HTML, CSS, or JavaScript - if you come to the party with those technologies the framework gets out of the way and lets you leverage them. If not, then thankfully it doesn't try to help you to pretend they don't exist.

ASP。所有这些废话都被删除了。您不会受到HTTP、HTML、CSS或JavaScript的屏蔽——如果您使用这些技术参加聚会,框架就会为您提供便利,并允许您利用它们。如果没有,那么值得庆幸的是,它并没有试图帮助你假装它们不存在。

#3


8  

Not a complete answer, but one big concern is testability of ASP.NET Forms, or the lack thereof. Testing the UI logic of what gets displayed and how. With ASP.NET Forms, you are left to just the codebehind.

虽然不是一个完整的答案,但是一个很大的问题是ASP的可测试性。NET表单,或缺少它。测试显示的内容和方式的UI逻辑。ASP。NET表单,只剩下代码了。

Now, MVC isn't for everyone. You may want to look into MVP (Model-View Presenter) for ASP.NET Forms as it uses very similar MVC concepts, except the Presenter is in control of changing the view's internals.

MVC并不适合所有人。您可能需要为ASP查看MVP(模型-视图演示者)。NET表单使用了非常相似的MVC概念,但是演示者可以控制改变视图的内部结构。

But, testability is really a big plus for testing your code. Such as whawt happens when someone clicks the ChangePassword method/action:

但是,可测试性确实是测试代码的一大优点。例如当某人单击ChangePassword方法/操作时发生的whawt:

[TestClass]
public class AccountControllerTest
{

    [TestMethod]
    public void ChangePasswordPostRedirectsOnSuccess()
    {
        // Arrange
        AccountController controller = GetAccountController();

        // Act
        RedirectToRouteResult result = 
            (RedirectToRouteResult)controller.ChangePassword(
                "oldPass", "newPass", "newPass");

        // Assert
        Assert.AreEqual("ChangePasswordSuccess"
            , result.RouteValues["action"]);
    }
}

#4


8  

One thing (out of many) that I like about MVC is that it gets rid of Web Server Controls. While they are seen by many as a great thing about WebForms, I have found that once you get past the basic operations they become a nightmare. Trying to juggle databinding events on grids with postbacks and everything else becomes the OO version of spaghetti code.

我喜欢MVC的一个原因是它摆脱了Web服务器控件。虽然它们被许多人视为WebForms的一大优点,但我发现,一旦你通过了基本操作,它们就会变成一场噩梦。尝试在网格上使用回发和其他所有东西来对事件进行数据绑定,这就变成了面向对象的意大利面代码。

MVC will require you have a better knowledge of the basic tenants of web development (GET, POST, REQUEST, HTML, CSS, JAVASCRIPT), the result will be much better. See my graph of how I think MVC works :-)

MVC将要求您更好地了解web开发的基本租户(GET、POST、REQUEST、HTML、CSS、JAVASCRIPT),结果将会更好。看看我的图,我认为MVC工作:

alt text http://www.baseestate.com/webformsmvc.gif

alt文本http://www.baseestate.com/webformsmvc.gif

#5


2  

It's true that you need to do more in MVC to get some of the same basic VERY automatic functionality you became accustomed to in WebForms.

确实,您需要在MVC中做更多的工作,以获得您在WebForms中习惯的一些基本的、非常自动的功能。

However, in the long run you end up with more control.

然而,从长远来看,你最终会拥有更多的控制权。

The main thing broken about WebForms is the whole PostBack scenario, and how many hoops you have to jump through to implement something simple WebForms didn't think of. One excellent example is my WebForms-related question: Is there any native way in ASP.NET to do a "success message"?

WebForms失败的主要原因是整个回发场景,以及为了实现一些简单的WebForms没有想到的东西,你需要跨越多少障碍。一个很好的例子是我的webforms相关的问题:在ASP中是否有原生的方式。NET做一个“成功信息”?

I wanted to make a "Record Saved" or "New Record Added" message in a WebForms application. Turns out you have to do some really scary stuff just to get this simple functionality to work, because they don't have a normal way to do this in WebForms, and to do a custom method, you're fighting against the hidden functionality in PostBack.

我想在WebForms应用程序中创建“保存记录”或“添加新记录”消息。为了让这个简单的功能正常工作,你必须做一些非常可怕的事情,因为他们没有在WebForms中做这个的常规方法,而要做一个自定义方法,你需要与回发中隐藏的功能做斗争。

In MVC, this would be no problem. You'd still have to write the functionality manually, but you'd have much more control over your page state.

在MVC中,这没问题。您仍然需要手动地编写功能,但是您可以对页面状态有更多的控制。

#6


2  

I believe you can only feel the difference after you've been in a big project and seen how much mess WebForms approach could cause.

我相信,只有在您参与了一个大型项目并看到WebForms方法可能导致的混乱之后,您才能感受到这种差异。

When you've seen a page contains multiple components, user controls, some of them living independent lives like hiding/showing or enabling/disabling themselves, all these rules passing through multiple control layers which cannot be traced until you've been in a project for many long years (or at least have smoked something reeealy good before diving in with the debugger :) ), you begin to appreciate the possibility for a clearly defined flow of control when you see how your data defines what components with what properties are rendered on a page.

当你看到一个页面包含多个组件,用户控件,其中一些独立生活像隐藏/显示或启用/禁用,所有这些规则通过多个控制层无法追踪,直到你在多年的项目(或者至少抽一些reeealy好之前在调试器:)),当您看到您的数据如何定义具有哪些属性的组件时,您将开始认识到控制流的可能性。

I've also loved WebForms for a long time. I also disliked first few weeks/months MVC for exactly same reasons you put forth. After I've done some coding and could compare the experience, I began to enjoy MVC.

我也喜欢WebForms很久了。我也不喜欢最初的几个星期或几个月的MVC,原因和你说的一模一样。在我完成了一些编码并可以比较这些体验之后,我开始喜欢MVC。

Still, there are some areas where MVC approach would be much more complicated and create more mess than WebForms would.

尽管如此,在某些领域,MVC方法会比WebForms更复杂,而且会产生更多的混乱。

It'll come with time. Not really a long time. :)

它会随着时间的推移。不是很长时间。:)

#7


2  

Of course a simple example is going to be very clear and readable no matter how you do it. Supposedly, MVC's advantage becomes more apparent as you scale up the complexity.

当然,不管你怎么做,一个简单的例子都是非常清晰易读的。按理说,随着复杂度的增加,MVC的优势会变得更加明显。

Also, it turns out that the webforms model just isn't that great for high-volume public internet sites. ViewState plus the expensive page life cycle in a normal web forms site can make scalability a challenge (not impossible, but challenging). By contrast, on an intranet site users generally have much greater upstream bandwidth (ViewState works better) and volume is much better controlled. So webforms really works great there.

而且,事实证明webforms模式对于高容量的公共网站来说并不是那么好。ViewState加上普通web表单站点中昂贵的页面生命周期可以使可伸缩性成为一种挑战(并非不可能,而是具有挑战性)。相比之下,在内部网站点上,用户通常拥有更大的上游带宽(ViewState工作得更好),而且音量控制得更好。所以webforms在那里非常好用。

#8


0  

I'm also taking a wait-and-see attitude about ASP.NET MVC (along with the Entity framework and WPF for diff reasons). I'm a huge fan of MVC in general but am a little concerned about wrapping something as general purpose as a web development framework into the constraints of one pattern. A very useful pattern but still only 1 of many.

我对ASP也持观望态度。NET MVC(连同实体框架和WPF)。总的来说,我是MVC的超级粉丝,但我有点担心将web开发框架这样的通用功能封装到一个模式的约束中。这是一个非常有用的模式,但仍然只是众多模式中的一个。

A skilled developer can implement MVC in practically any language. So MVC may be a great way to prevent less-skilled developers (we're all this at some point, like when just learning a framework) from making egregious mistakes. Since the pattern works well in a wide range of scenarios that could make it a net benefit.

熟练的开发人员可以在任何语言中实现MVC。因此,MVC可能是防止低技能开发人员(我们在某种程度上都是这样,比如学习框架)犯严重错误的好方法。由于模式在广泛的场景中都能很好地工作,因此可以使它成为一个净收益。

On the other hand, for expert developers it may turn out to be like training wheels on an olympic cyclist... Redundant and more of a pain than a boon.

另一方面,对于专业开发人员来说,它可能就像训练奥运自行车*一样……多余的,痛苦多于恩惠。

#9


0  

I think it very much depends upon your background. If you're already familiar with something like Ruby-rails and/or Django, asp.net mvc makes much more sense.

我认为这在很大程度上取决于你的背景。如果您已经熟悉诸如Ruby-rails和/或Django之类的东西,那么asp.net mvc就更有意义了。

Also if you have been doing websites in Html and css for a long time, Mvc is much better as you have full control over your html output.

同样,如果你用Html和css做网站已经很长时间了,Mvc会更好,因为你可以完全控制你的Html输出。

If you're comfortable with asp.net just keep using that, it's not going away :).

如果你喜欢asp.net,那就继续使用它,它不会消失。

#1


14  

ASP.NET MVC Best Practices, Tips and Tricks

ASP。NET MVC的最佳实践、技巧和技巧

I would write:

我想写:

<h2><%= Html.Encode(Model.Title) %></h2>

(it's possible with a help of typed views)

(可以借助类型化视图)

instead of

而不是

<h2><%= Html.Encode((MyApp.MyObject)ViewData["PageObject"].Header) %></h2>

I think it's all about how you use it. If you're more happy with classic ASP.NET, than maybe it will be a better idea to stick with it. Moreover, you could also take good stuff from ASP.NET MVC world (like UI and Logic separation) and bring it to the classic ASP.NET.

我认为关键在于你如何使用它。如果你对经典的ASP更满意的话。NET,也许坚持它会是一个更好的主意。此外,您还可以从ASP中获取好的内容。NET MVC世界(像UI和逻辑分离),并将它带到经典的ASP.NET中。

#2


10  

What's great about ASP.NET MVC is that is doesn't try to hide how HTTP works. To fully understand ASP.NET MVC you need to understand the technologies of the web.

有什么伟大的ASP。NET MVC并没有试图隐藏HTTP是如何工作的。充分了解ASP。您需要了解web的技术。

While webforms are adequate as long as you work to their strengths, they're ultimately a very leaky abstraction when you don't. While the drawbacks of viewstate have been well discussed by this point I think it's the extremely unwise attempt to mimic the behaviour of Winforms that is the underlying flaw - viewstate is merely a product of that.

虽然只要您发挥它们的长处,web表单就足够了,但如果不这样做,它们最终将是一个非常漏洞百出的抽象。虽然viewstate的缺点已经在这一点上得到了很好的讨论,但我认为,模仿Winforms的行为是潜在的缺陷——viewstate仅仅是这个缺陷的产物。

The web controls which ship with ASP.NET also leave a (hell of a) lot to be desired as anyone who has tried to build an accessible website can attest to. The web controls show a total lack of understanding for how frontend development is done, and frankly are a disgrace.

web控制使用ASP发送哪些内容。NET也有很多值得期待的地方,任何尝试建立一个可访问网站的人都可以证明这一点。web控件显示完全不了解前端开发是如何完成的,坦率地说,这是一种耻辱。

With ASP.NET MVC all that nonsense is done away with. You're not shielded from HTTP, HTML, CSS, or JavaScript - if you come to the party with those technologies the framework gets out of the way and lets you leverage them. If not, then thankfully it doesn't try to help you to pretend they don't exist.

ASP。所有这些废话都被删除了。您不会受到HTTP、HTML、CSS或JavaScript的屏蔽——如果您使用这些技术参加聚会,框架就会为您提供便利,并允许您利用它们。如果没有,那么值得庆幸的是,它并没有试图帮助你假装它们不存在。

#3


8  

Not a complete answer, but one big concern is testability of ASP.NET Forms, or the lack thereof. Testing the UI logic of what gets displayed and how. With ASP.NET Forms, you are left to just the codebehind.

虽然不是一个完整的答案,但是一个很大的问题是ASP的可测试性。NET表单,或缺少它。测试显示的内容和方式的UI逻辑。ASP。NET表单,只剩下代码了。

Now, MVC isn't for everyone. You may want to look into MVP (Model-View Presenter) for ASP.NET Forms as it uses very similar MVC concepts, except the Presenter is in control of changing the view's internals.

MVC并不适合所有人。您可能需要为ASP查看MVP(模型-视图演示者)。NET表单使用了非常相似的MVC概念,但是演示者可以控制改变视图的内部结构。

But, testability is really a big plus for testing your code. Such as whawt happens when someone clicks the ChangePassword method/action:

但是,可测试性确实是测试代码的一大优点。例如当某人单击ChangePassword方法/操作时发生的whawt:

[TestClass]
public class AccountControllerTest
{

    [TestMethod]
    public void ChangePasswordPostRedirectsOnSuccess()
    {
        // Arrange
        AccountController controller = GetAccountController();

        // Act
        RedirectToRouteResult result = 
            (RedirectToRouteResult)controller.ChangePassword(
                "oldPass", "newPass", "newPass");

        // Assert
        Assert.AreEqual("ChangePasswordSuccess"
            , result.RouteValues["action"]);
    }
}

#4


8  

One thing (out of many) that I like about MVC is that it gets rid of Web Server Controls. While they are seen by many as a great thing about WebForms, I have found that once you get past the basic operations they become a nightmare. Trying to juggle databinding events on grids with postbacks and everything else becomes the OO version of spaghetti code.

我喜欢MVC的一个原因是它摆脱了Web服务器控件。虽然它们被许多人视为WebForms的一大优点,但我发现,一旦你通过了基本操作,它们就会变成一场噩梦。尝试在网格上使用回发和其他所有东西来对事件进行数据绑定,这就变成了面向对象的意大利面代码。

MVC will require you have a better knowledge of the basic tenants of web development (GET, POST, REQUEST, HTML, CSS, JAVASCRIPT), the result will be much better. See my graph of how I think MVC works :-)

MVC将要求您更好地了解web开发的基本租户(GET、POST、REQUEST、HTML、CSS、JAVASCRIPT),结果将会更好。看看我的图,我认为MVC工作:

alt text http://www.baseestate.com/webformsmvc.gif

alt文本http://www.baseestate.com/webformsmvc.gif

#5


2  

It's true that you need to do more in MVC to get some of the same basic VERY automatic functionality you became accustomed to in WebForms.

确实,您需要在MVC中做更多的工作,以获得您在WebForms中习惯的一些基本的、非常自动的功能。

However, in the long run you end up with more control.

然而,从长远来看,你最终会拥有更多的控制权。

The main thing broken about WebForms is the whole PostBack scenario, and how many hoops you have to jump through to implement something simple WebForms didn't think of. One excellent example is my WebForms-related question: Is there any native way in ASP.NET to do a "success message"?

WebForms失败的主要原因是整个回发场景,以及为了实现一些简单的WebForms没有想到的东西,你需要跨越多少障碍。一个很好的例子是我的webforms相关的问题:在ASP中是否有原生的方式。NET做一个“成功信息”?

I wanted to make a "Record Saved" or "New Record Added" message in a WebForms application. Turns out you have to do some really scary stuff just to get this simple functionality to work, because they don't have a normal way to do this in WebForms, and to do a custom method, you're fighting against the hidden functionality in PostBack.

我想在WebForms应用程序中创建“保存记录”或“添加新记录”消息。为了让这个简单的功能正常工作,你必须做一些非常可怕的事情,因为他们没有在WebForms中做这个的常规方法,而要做一个自定义方法,你需要与回发中隐藏的功能做斗争。

In MVC, this would be no problem. You'd still have to write the functionality manually, but you'd have much more control over your page state.

在MVC中,这没问题。您仍然需要手动地编写功能,但是您可以对页面状态有更多的控制。

#6


2  

I believe you can only feel the difference after you've been in a big project and seen how much mess WebForms approach could cause.

我相信,只有在您参与了一个大型项目并看到WebForms方法可能导致的混乱之后,您才能感受到这种差异。

When you've seen a page contains multiple components, user controls, some of them living independent lives like hiding/showing or enabling/disabling themselves, all these rules passing through multiple control layers which cannot be traced until you've been in a project for many long years (or at least have smoked something reeealy good before diving in with the debugger :) ), you begin to appreciate the possibility for a clearly defined flow of control when you see how your data defines what components with what properties are rendered on a page.

当你看到一个页面包含多个组件,用户控件,其中一些独立生活像隐藏/显示或启用/禁用,所有这些规则通过多个控制层无法追踪,直到你在多年的项目(或者至少抽一些reeealy好之前在调试器:)),当您看到您的数据如何定义具有哪些属性的组件时,您将开始认识到控制流的可能性。

I've also loved WebForms for a long time. I also disliked first few weeks/months MVC for exactly same reasons you put forth. After I've done some coding and could compare the experience, I began to enjoy MVC.

我也喜欢WebForms很久了。我也不喜欢最初的几个星期或几个月的MVC,原因和你说的一模一样。在我完成了一些编码并可以比较这些体验之后,我开始喜欢MVC。

Still, there are some areas where MVC approach would be much more complicated and create more mess than WebForms would.

尽管如此,在某些领域,MVC方法会比WebForms更复杂,而且会产生更多的混乱。

It'll come with time. Not really a long time. :)

它会随着时间的推移。不是很长时间。:)

#7


2  

Of course a simple example is going to be very clear and readable no matter how you do it. Supposedly, MVC's advantage becomes more apparent as you scale up the complexity.

当然,不管你怎么做,一个简单的例子都是非常清晰易读的。按理说,随着复杂度的增加,MVC的优势会变得更加明显。

Also, it turns out that the webforms model just isn't that great for high-volume public internet sites. ViewState plus the expensive page life cycle in a normal web forms site can make scalability a challenge (not impossible, but challenging). By contrast, on an intranet site users generally have much greater upstream bandwidth (ViewState works better) and volume is much better controlled. So webforms really works great there.

而且,事实证明webforms模式对于高容量的公共网站来说并不是那么好。ViewState加上普通web表单站点中昂贵的页面生命周期可以使可伸缩性成为一种挑战(并非不可能,而是具有挑战性)。相比之下,在内部网站点上,用户通常拥有更大的上游带宽(ViewState工作得更好),而且音量控制得更好。所以webforms在那里非常好用。

#8


0  

I'm also taking a wait-and-see attitude about ASP.NET MVC (along with the Entity framework and WPF for diff reasons). I'm a huge fan of MVC in general but am a little concerned about wrapping something as general purpose as a web development framework into the constraints of one pattern. A very useful pattern but still only 1 of many.

我对ASP也持观望态度。NET MVC(连同实体框架和WPF)。总的来说,我是MVC的超级粉丝,但我有点担心将web开发框架这样的通用功能封装到一个模式的约束中。这是一个非常有用的模式,但仍然只是众多模式中的一个。

A skilled developer can implement MVC in practically any language. So MVC may be a great way to prevent less-skilled developers (we're all this at some point, like when just learning a framework) from making egregious mistakes. Since the pattern works well in a wide range of scenarios that could make it a net benefit.

熟练的开发人员可以在任何语言中实现MVC。因此,MVC可能是防止低技能开发人员(我们在某种程度上都是这样,比如学习框架)犯严重错误的好方法。由于模式在广泛的场景中都能很好地工作,因此可以使它成为一个净收益。

On the other hand, for expert developers it may turn out to be like training wheels on an olympic cyclist... Redundant and more of a pain than a boon.

另一方面,对于专业开发人员来说,它可能就像训练奥运自行车*一样……多余的,痛苦多于恩惠。

#9


0  

I think it very much depends upon your background. If you're already familiar with something like Ruby-rails and/or Django, asp.net mvc makes much more sense.

我认为这在很大程度上取决于你的背景。如果您已经熟悉诸如Ruby-rails和/或Django之类的东西,那么asp.net mvc就更有意义了。

Also if you have been doing websites in Html and css for a long time, Mvc is much better as you have full control over your html output.

同样,如果你用Html和css做网站已经很长时间了,Mvc会更好,因为你可以完全控制你的Html输出。

If you're comfortable with asp.net just keep using that, it's not going away :).

如果你喜欢asp.net,那就继续使用它,它不会消失。