HowTo: Basics of ASP.NET MVC or why MVC?

A while ago I blogged about some ASP.NET MVC stuff, but why should I (and you) care about ASP.NET MVC?
ASP.NET MVC is a great and extensible framework for building web applications and is a alternative to the ASP.NET WebForms model.

Tell me more about "MVC"! 
MVC stands for "Model-View-Controller", which is a very old (but still very useful) design pattern. This design pattern will split your application in 3 different parts ("model", "view", "controller"). "Seperation of concern" is one of it´s main benefits.

image_thumb2 

Short description of these parts:

  • "Model": Represents your application data/model and has no(!) business logic
  • "View": Just display the given viewdata (this could be a normal HTML page or JSON/RSS data)
  • "Controller": The controller represents the business logic and create the view data and send it to a view.

A good example of an MVC application is the web browser.

What is so "bad" about ASP.NET WebForms?
ASP.NET WebForms include many abstractions for the web development. If you are a WinForms developer, you will feel comfortable with it, but if you started with PHP/JSP or just pure HTML and Javascript you will feel very uncomfortable.
The "viewstate" is one feature to hide the stateless nature of HTML, but it can make your web application very slow and makes you crazy. The "WebForms" model include a very complex lifecycle and it´s  important to understand this to work with ASP.NET WebForms.
In my opinion it is too complex and the framework should embrace the nature of HTTP and don´t hide it.

Disclaimer: If you feel comfortable with WebForms, you have no reason to change to MVC – MVC is only on option.

What are the benefits of ASP.NET MVC
MVC is a very testable framework and you get full control of the rendering process. You can add functionality if you want, because MVC is very extensible and you can create a clean, DRY, testable web application.
Phil Haack (the program manager of ASP.NET MVC) did a great presentation at the PDC.

Things that you´ll maybe missing in MVC
Many ASP.NET controls use the postback-functionality. This function will not work well together with the MVC framework. Phil Haack did also 2 podcasts on HerdingCode and tell some thought about "the control story in MVC":
Part 1
Part 2

If you have questions, please add a comment (and if my english really suck please let me know ;) ). This blogpost should only provide basics – other posts are planned.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by

Currently there is no additional info about this author.

  • Pingback: Dew Drop - November 25, 2008 | Alvin Ashcraft's Morning Dew

  • Pingback: HowTo: First steps with ASP.NET MVC | Code-Inside Blog International

  • Pingback: Fresh Links November 26th, 2008 | News | Server-Side Magazine

  • Pingback: Interesting Finds: 2008.11.31 - gOODiDEA.NET

  • http://www.thirdarm.net.nz Simon

    Umm, from what I’ve read the business logic should all be in the model (which could infact be a web service and not necessarily a just a database). For example, the ASP.NET MVC screencasts at http://www.asp.net/mvc/ and Rob Conery’s storefront all state that the controllers should be kept lightweight and really are there just for flow control. Apparently they should contain logic for directing between views and little more. Conversly, the model should contain the domain logic, constraints, data access and everything else because it is easy to test and easy to reuse (with other front ends).
    Can you explain why you said in your post that the model should contain no(!) business logic?

  • http://code-inside.de Robert Muehsig

    You are right – the controller should be very light weight. But the word “model” is a bit unspecific. Model is for me only simple classes that contain your application objects, e.g. :

    public class User
    {
    public string Name { get; set; }
    }

    I have 4 C# projects in my solution:
    MyProject.Data for data access
    MyProject.Services for business logic
    MyProject.WebApp the MVC Website
    MyProject.Model for my simple application classes like the user class

    The last project contains no business logic – and in the MVC “Model” folder is nothing. The whole business logic is inside the services layer. That´s why I said, that in the model is no business logic.

    See at Rob Conerys MVCStorefront project – it´s much like that :)

  • Pingback: Fresh Links November 26th, 2008 | Server-Side Magazine

  • http://blingee.com/profile/wrenbra6 new no deposit bingo sites

    What’s up, I want to subscribe for this web site to obtain newest updates, therefore where can i do it please help.

Recent Posts

  • HowTo: make out Retweet-counter in a random URL

      Twitter offers some interesting functions with the integration of the twitter button – every user can see how often the website has been retweeted and he can do the same with just one click. But of course the whole thing has his price: - The integration of the Twitter JavaScript makes the whole data […]

  • Time-controlled Azure WebJobs – Azure as easy as it get‘s

        While still in development the Azure WebJob SDK offers some cool features for procession and supply of information. A good example is the sample that observes the Azure Queue and processes an item as soon as it spots one.   Scenario: time-controlled activities – without queue and so on My scenario was quite […]

  • image2021-570x194_thumb.png
    HowTo: Create RSS Feeds with ASP.NET MVC

    I’ve already written about how to consume RSS or Atom Feeds with the SyndicationFeed (in German, sry) but todays post is about how to create or publish an own feed. Beside the age of the feed standard and the much defamation from Facebook, Twitter and Google on RSS/Atom I consider this easy to consume API […]

  • image1997-311x194_thumb.png
    How to access an Azure Website with the local IIS Manager

    Since the end of February it is possible to access an Azure Website with the IIS Manager. Although the Azure Management site offers some information there are more details visible at the IIS Manager. For the connection you will need an IIS Manager and the IIS Manager for Remote Administration Extension. It’s also possible to […]

  • image1929-570x143_thumb.png
    Create and validate own Json-Web-Tokens (JWTs)

    If you are interested in web authentication you probably have heard about JSON Web tokens (JWT). What is a JWT? Maybe I’m not using the correct security termination but however: JWTs are used to exchange claims between two systems. For example: You want to log on to a service (like Facebook, Twitter, etc.) and want […]

Support us