22

One of the common questions asked regarding ASP.NET MVC is why should you use it over ASP.NET Web Forms? The answer generally includes ViewState and clean URLs, amongst others. Towards the end you'll find a blurb about using the right tool for the job and that they serve different purposes. However, I don't believe I've ever seen what those purposes are. So, when would you actually choose ASP.NET MVC over ASP.NET Web Forms, or ASP.NET Web Forms over ASP.NET MVC?

bdukes
  • 152,002
  • 23
  • 148
  • 175
Chris Stewart
  • 3,303
  • 9
  • 36
  • 55
  • 2
    This is a duplicate of http://stackoverflow.com/questions/41712/traditional-asp-net-vs-mvc and many, many more. I just searched http://stackoverflow.com/search?q=When+ASP.NET+MVC+ASP.NET+Web+Forms and found them. – John Saunders Jul 17 '09 at 16:41
  • And so the self-fulfilling prophecy is complete! This question now appears at the top of your search @JohnSaunders :) – JumpingJezza Feb 03 '15 at 02:09

6 Answers6

29

You don't choose ASP.Net MVC over ASP.Net, because ASP.Net MVC still is ASP.Net. You do choose ASP.Net MVC or ASP.Net Web Forms, and there are lots of good reasons to do that:

  • Easier to get control over your HTML
  • Easier to do unit testing
  • Few "Gotchas"

On the other hand, Web Forms do have a few points in their favor:

  • Easy to put simple CRUD/business apps together extremely fast
  • Hard to beat ViewState performance in local LAN environment
  • Easy to learn forms paradigm

The result is that if you're building business apps in a corporate LAN environment (which honestly is still most web developers), Web Forms is really great. In that sense Microsoft really knows their market. But if you're building an app for the public internet, you might want MVC so you can test thoroughly and be sure your pages aren't bloated with unnecessary ViewState or JavaScript data.

Additionally, something that has changed over the last several years is that even many corporate intranet applications now need to support home/remote use, making MVC more attractive to that crowd than it had been.

Robert
  • 1,286
  • 1
  • 17
  • 37
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • I'm curious about your statement about "hard to beat viewstate performance in local LAN environment" - can you clarify? Are you suggesting it's network burden is less when local so you don't have to worry about network performance as much? – Matt Jul 17 '09 at 16:07
  • 4
    Viewstates can get very large, and this is definitely a performance issue when the server is not local. – Steven Sudit Jul 17 '09 at 16:09
  • 4
    The absolute network burden is the same: a 60K viewstate is 60K everywhere. The _relative_ network burden is very differetn: 60K to a local network is nothing, but 60K uploaded for every postback over even a consumer broadband connection can make your pages appear much slower than they are. – Joel Coehoorn Jul 17 '09 at 16:23
  • 3
    Also, if your viewstate is absolutely HUGE, you're doing something wrong. – Joel Coehoorn Jul 17 '09 at 16:24
  • 1
    Take issue with "Easy to learn forms paradigm". (Compare request/response with the forms page lifecycle.) Also: Webforms leaves little control to the developer to manage complexity. – Steve Horn Oct 03 '09 at 17:33
10

Use MVC if all your team members are skilled enough to manage "control over HTML", otherwise your code will turn into a tag soup.

In other words

bool useMvc = true;
foreach (TeamMember member in team.Members)
{
    useMvc = useMvc && member.IsSkilled;
} 
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
4

http://weblogs.asp.net/shijuvarghese/archive/2008/07/09/asp-net-mvc-vs-asp-net-web-form.aspx

check that blog !

Bottom line "separation of concerns"

Broken Link
  • 2,396
  • 10
  • 30
  • 47
4

I'll give you a couple purposes, with clear advantages.

  • If your purpose is a public facing website that will be banking on traffic, use MVC. It is optimal for search engine optimization.

  • If your purpose is an enterprise web-application that acts like a desktop app, I would lean towards web forms, since state management and compartmentalization of your resources into underlying server controls offers huge advantages if used correctly.

womp
  • 115,835
  • 26
  • 236
  • 269
  • 1
    Whether or not the site will be easily recognized by search engines is something that is frequently left out of design considerations. Good point on it being an MVC benefit. – Kivus Jul 17 '09 at 16:26
  • 2
    Search engine optimization should no longer be a deciding factor in the MVC vs WebForms debate. **ASP.NET 4.0** now allows you to also use the URL Routing engine to **map URLs to ASP.NET Web Forms pages** as well as ASP.NET MVC Controllers. [See this link for details](http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx) – JTech Oct 13 '12 at 13:37
3

The biggest problems facing developers is managing complexity and keeping code "clean". MVC gives the developer the reins to leverage OOP to tuck away complexity and make code easy on the eyes.

Webforms will be faster to develop in the short term, but it doesn't lend itself to long term sustainability in terms of maintenance and growth.

Steve Horn
  • 8,818
  • 11
  • 45
  • 60
2

I've worked with Web forms for 13 years and MVC for 2 years now and when I started with MVC, I had similar questions. Here are my takeaways.

  • Most importantly: ASP.NET's latest release is 4.6 and they were moving to ASP.NET 5.0, but MS abandoned that for ASP.NET Core, which no longer supports Web Forms (or even VB.NET). So, that alone might give you your answer when deciding what rabbit hole to go down.

That being said:

  • MVC I'm finding, once you get the hang of it, is WAY easier for dealing with basic forms and any sort of simple "Model", aka tables with a very simple, straight-forward set of relationships such as orders that have tables that link to users, products, etc. Once you start getting into some more complicated relationships and need to return lots of conditional sets of results, rely on parameters, have complicated stored procedures... then Web Forms is much better for dealing with this. If you don't have to deal with this level of complication, MVC makes development SO MUCH faster, especially with dealing with an approach where you already have the DB because it creates so much of the code and validation for you already

  • If you're not very experienced with database design, MVC does the work for you. It can literally build the database for you.

  • MVC doesn't have a lot of the built in controls that Web Forms does (Gridviews, FormViews, Sitemaps, Paged lists). Everything has to be written from scratch, but luckily a lot of people have already invented that stuff for you in NuGet which you can just download into your project

  • MVC relies heavily on the structure of your URL. The path, the querystring, etc. If you find your application needing to do a lot of form POSTing instead of GET-ting, you're going to have to do a lot of tweaking or AJAX posting. If you have a set URL that can't change, it can be a pain. It's doable, but just a little tricker (or you can just use Angular instead).

  • MVC has no Viewstate. If you need to hide variables from post to post and persist them, it's a little difficult. MVC Does have things like ViewBag which lets you pass data from your controller to your page, but it clears after the page is rendered. There is also something called "Tempdata" which acts like Session state, but more temporary. However, it relies on Session State which is not an ideal way of persisting data. Session variables and tempdata variables are fine for user-level data (profile information for the person logged in), but having two different tabs open by the same user can cause these session/tempdata variables to overwrite each other when you're dealing with the actual model data.

If you're at a crossroads, I'd go with MVC. MS is pushing it and support for Web Forms will likely start going away

citronsmurf
  • 90
  • 11