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 Code Inside Team

Currently there is no additional info about this author.

8 Responses

  1. 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?

    Reply
  2. 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 :)

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

    Reply

Comment on this post

Recent Posts

  • image1825-439x194_thumb.png
    Change the WebDeploy Port or why do I need port 8172?

      If you use WebDeploy on a server operation system you would usually use Port 8172. But what is this Port for and can I change it? Hint: For installing the WebDeploy I recommend this Blogpost. Port 8172 = IIS Management Service Default Port The Port 8172 is the default port of the IIS Management ...

  • Windows Phone Fonts & what if Visual Studio lies

    Today I was confronted with a little Problem: my Windows Phone App refused to show me the Font I choose – also other thinks didn’t work. Although the Visual Studio Designer did show the Fonts: Unfortunately there isn’t much left in the Emulator: Reason for this: Windows Phone doesn’t include all the typos Windows does ...

  • Json-Online-Tools: Viewer & Json2Csharp generator

      Wherever APIs are mentioned the JSON format I not far away. Since I’m using two tools regularly I would like to introduce you to them. JSON Viewer If you only see the JSON-Text you are usually not able to see the structure. With the help of JSON Viewer you can have an easy overview: ...

  • Windows Phone SDK & „System“-Icons

      Although the Metro Design focuses a lot on Typography Icons are still quite important. If you install Windows Phone SDK you will receive 36 Icons. You can find them here: C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons Unfortunately many Icons you might know from the common applications are not integrated. Pedro Lamas extracted 99 additional Icons ...

  • image1830-570x194.png
    How can I figure out if my ADFS 2.0 works?

      I was working with ADFS 2.0 (“Active Directory Federation Services”) for a while when this simple question crossed my mind: How can I figure out if the connection between ADFS and AD “works”? Here is a simple test… What is ADFS? If you need some “position of trusts” beneath the AD-boarders you choose an ...

Support us