ASP.NET MVC or webforms? VB or C#? We help you choose

Choosing the right ASP.NET training course is tricky.  You've got two important decisions to make:

  1. Should you program in Visual Basic or Visual C#?
  2. Should you use ASP.NET webforms or ASP.NET MVC?

We've covered the first choice in a separate page (reluctant answer: go with C#), so this page will concentrate on the differences between the two versions of ASP.NET. 

The following table shows you which courses you should choose after you've made your decision:

Version Go with this course
ASP.NET MVC 3-day ASP.NET webforms for VB or for C#
ASP.NET webforms 4-day ASP.NET MVC (for C# only)

To illustrate the differences between the two approaches, I'll show the example of getting a webpage to display a list of films:

List of films

The list of films as it will appear in MVC (the webforms version will be the same, but with more formatting).

 

We'll start with the classic ASP.NET approach, then show how the MVC one differs.

For a longer look at what creating ASP.NET MVC websites involves, take a look at this blog.

The ASP.NET Webforms approach

To create this webpage in ASP.NET webforms, you might drag a gridview onto your page:

Adding a gridview

Adding a gridview to a form is straightforward - just drag and drop the appropriate tool.

A few clicks in a wizard gives you this:

The gridview datasource

You can assign a data source to a gridview easily, using a wizard.

When you browse to this, you'll see something like this:

Gridview in browser

The formatting comes from a theme created for this website.

 

Here's the code-behind this page we've written to make it work (there isn't any!):

protected void Page_Load(object sender, EventArgs e)

{

}

So far, so easy: and if that was the end of the story, MVC wouldn't have been developed.  The problem comes when you want to fine-tune the HTML generated:

Gridview's underlying HTML

A small part of the gridview generated. The viewstate (shown partially selected) is a mind-boggling 5,450 characters long.

This sums up ASP.NET webforms: it's easy to generate systems, but hard to customise or tweak the HTML, since everything is done on the server.  Let's now look at the alternative - MVC - then examine the strengths and weaknesses of each.

The ASP.NET MVC approach

Here's a quick summary of the MVC solution to the same problem.  As mentioned above, there's a much longer blog about this, if you want more details.

MVC stands for Model View Controller, by the way.  Whereas a classic ASP.NET page is just that - a page - in MVC the situation is considerably more complex: a controller creates a model, and then chooses a view to display it.  That sentence isn't meant to make sense to the uninitiated!

Let's start with the HTML page to show the list of films:

The MVC equivalent of a gridview

The list of films in HTML, using MVC.

The first line of this declares that this page (it's actually not a page but a view, but never mind) is going to use a model called vwFilm:

The model used

The line in question.

 

This model is a class, which might look something like this:

public class vmFilm

{

// create a "data context" for this controller

protected MoviesEntities movies = new MoviesEntities();

public List<tblFilm> Films

{

get {

// return a set of films

return (

from f in movies.tblFilm

select f

).ToList();

}

}

}

But this class uses something called an entity model to extract data from a database:

The entity model used

The entity model of our films database (if only all databases had just one table like this one ...).

 

It also uses something called LINQ to extract data from the database:

// return a set of films

return (

from f in movies.tblFilm

select f

).ToList();

So let's make a list of what you've had to learn to create this example:

Thing learnt Notes
Routers and controllers How MVC decides which view to return (not shown above).
C# You'd be mad to learn ASP.NET MVC using VB, as you'd end up translating all of the helpful code given on the Internet.
Entity models You can use code-first, model-first or database-first techniques to get at your database, but you'll need to choose one of them.
LINQ A language for extracting information from tables, which is (almost) essential to learn for MVC.
Classes A view model is just a class, and boy do you need to understand classes to use MVC!

Above all else, you'll need to understand classes inside-out and back-to-front to create successful MVC websites.

Comparison of the two technologies

If you've read this far, you'll probably be thinking you should go for ASP.NET webforms, and for simple websites I think you'd be right (the learning curve is much shorter).

One sign that the learning curve is longer for MVC: our classic ASP.NET webforms course lasts 3 days, and gives a good grounding; our MVC course lasts four days, and gives an introduction only.

However, for larger websites (and I'd definitely count the Wise Owl one in that number), I'd choose MVC any time.  Which begs the question: why? 

First, lets look at the advantages of using webforms:

Advantage Notes
Simplicity It's easier to learn how to develop websites using ASP.NET webforms.
Legacy skills Classic ASP.NET fits in much better with most programmers' existing skills (if you've used Access, VB6 or Windows Forms, you'll be familiar with the idea of events, for example).
Programming needed Don't let anyone fool you - you will need to program to get a classic ASP.NET website to work.  However, you won't need to be a guru-level programmer, unlike for MVC.
Preserves state When you enter details into a form and post it back to the server, classic ASP.NET will automatically remember all of the details that you typed in.
HTML skills needed Classic ASP.NET provides something called web server controls, which are automatically translated into HTML, so that in theory you don't need to be an HTML guru to create classic ASP.NET websites.

Now the advantages of MVC:

Advantage Notes
Pure HTML In MVC, you're working directly with the final HTML; in classic ASP.NET, you're working with a glorified wizard which generates HTML on your behalf.  The MVC approach gives you much more control over your webpage.
Classes If you are an expert on using classes (and it's a big if), MVC will make you more productive, as you can integrate entity models and LINQ with your views to make everything easier and quicker to create.
Razor MVC allows you to mix C# code and HTML in the same page, with a clever algorithm detecting which language you're using at any time.

Conclusion and Recommendation

Our conclusion?  We would use:

  • MVC provided that either the development team already consisted of experienced object-oriented (or OO) programmers - with an excellent understanding of classes - or if it made sense as an organisation to develop OO skills;  or
  • Webforms for fairly simple websites (and especially where the development team has a classic programming background).

One caveat: if your website is going to use much JavaScript, AJAX or other client-side tools, or if accessibility and clean HTML is important to you, do yourself a favour and use MVC to get full control over your HTML.

This page has 0 threads Add post