Alex Rivera | Logout

ASP.NET MVC > ASP.NET WebForms, Why?

Asked 2009-04-06T18:37:13.110
13

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 Forms View Code:

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

Web Forms Code Behind:

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

MVC Code View:

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

MVC Controller Code:

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.

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

Edit
Report

2 Answers

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.

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.

answered 2009-04-06T18:42:31.927
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.

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"?

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.

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.

answered 2009-04-06T18:46:05.367

Your Answer