ASP.NET MVC> ASP.NET WebForms的,为什么呢? [英] ASP.NET MVC > ASP.NET WebForms, Why?

查看:191
本文介绍了ASP.NET MVC> ASP.NET WebForms的,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经完成了使用ASP.NET MVC我的第一个Web应用程序和整体的,我还是我不是抓这是为什么让所有的赞美和荣耀。也许是我固执。我知道,是什么让MVC伟大,是因为是presentation层和业务对象或数据层之间的分离势力与它的无状态的操作一起。我也知道,当你在视图中工作,现在看来,code是不可读(请参阅下面的示例所示)。

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).

所以我想我的问题是...如果分离是担忧,为什么不分开。

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

Web窗体查看code:

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

Web窗体code背后:

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

MVC code查看:

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

MVC控制器code:

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

同样,我可能会固执,但我真的很喜欢的WebForms的事情之一是在设置数据源像和文本值对象属性的难易程度。看起来这是MVC完全不同的,不易阅读这使我怀疑长期维护。

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.

修改
展望输入模型后,我觉得code的可读性大大提高。

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

推荐答案

ASP.NET MVC的最佳实践,技巧和窍门

我会写:

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

(它可能与类型的视图帮助)

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

而不是

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

我想这是所有关于你如何使用它。如果你更乐意与经典的ASP.NET,比也许这将是一个更好的主意,坚持下去。此外,你也可以采取好东西从ASP.NET MVC的世界(如UI和逻辑分离),并把它经典的ASP.NET。

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 MVC&GT; ASP.NET WebForms的,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆