Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 983
RSolberg Avatar asked Apr 06 '09 18:04

RSolberg


People also ask

Why MVC is better than ASP.NET Web Forms?

MVC provides better support to TDD (Test driven development). TDD is related to the test first programming concepts of extreme programming. It helps us to reduced time in reworks and helps create loosely coupled code. MVC enforces separation that reduces complexity of project structure.

When should I use ASP.NET MVC vs Web Forms?

Asp.Net MVC has Partial Views for code re-usability. Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development.

What are the advantages of MVC over traditional ASP Net webform application?

The MVC framework provides a clean separation of the UI , Business Logic , Model or Data. On the other hand we can say it provides Sepration of Program logic from the User Interface. More Control-The ASP.NET MVC framework provides more control over the HTML , JavaScript and CSS than the traditional Web Forms.

Why do we use Web Forms?

A web form is a medium that allows your visitors to get in contact with you and to send information, such as an order, a catalog request, or even a query, which is passed on to your database. On some websites, information that is submitted through web forms is transferred directly to the company e-mail.


2 Answers

ASP.NET MVC Best Practices, Tips and Tricks

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.

like image 56
Konstantin Tarkus Avatar answered Nov 15 '22 22:11

Konstantin Tarkus


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.

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.

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.

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.

like image 41
derek lawless Avatar answered Nov 15 '22 22:11

derek lawless