How ASP.NET MVC works

How does ASP.NET MVC work? In following post you can learn how it works and that its magic – the loose coupling – comes from convention.

Why do i say magic? Because with the loose coupling of its components Model, View and Controller the code is way more readable and testable (to name only two).

How ASP.NET MVC works

The convention of ASP.NET MVC is that you can set a name in one component just like you want. But you have to repeat this name exactly in the other component.

A home controller

This is an example of a main controller that covers pages like Home, About, Privacy Policy, etc. In following image Home is such a name. You can choose the name by your own, but you have to repeat it exactly: Home2Controller won’t work (the convention sometimes adds a keyword like Controller). Another example for such a name is About.

  1. User clicks on button About, sends get request with URL [Path]/Home/About
  2. MVC searches for the HomeController
  3. Retrieves model (in our case empty)
  4. Looks for the view About
  5. Browser receives answer from MVC application and shows page with URL [Path]/Home/About

Please click on the image to see it full size:

How ASP.NET MVC works - home controller
How ASP.NET MVC works – home controller *

 

A list controller

Another typical use case you can find in many tutorials is a list controller. In following example I refer to the tutorial Getting Started with ASP.NET MVC 5 from the ASP.NET page.

The controller handles list overview views and list item views. Lets say the user requests a list item with the id = 1. That means

  1. User clicks on URL www.yourdomain.com/movies/details/1 –
    = sends get request with URL [Path]/[Controller]/[ActionName]/[Parameters]
  2. MVC searches for the MoviesController
  3. Retrieves model – in our case a movies database item with the id 1
  4. Looks for the view Details
  5. Controller sends the view Details containing the retrieved model (in Razor: code line “@model“) to browser, a page is shown with URL [Path]/movies/details/id

Then you got:

How ASP.NET MVC works - list controller
How ASP.NET MVC works – list controller *

 

I hope this helped a little bit to clarify how ASP.NET MVC works (it helped me a lot and i use it often when I do some tutorials).

Further Links

For a beginner tutorial I recommend following ASP.NET MVC Getting Started tutorial: http://www.asp.net/mvc/overview/getting-started/introduction/getting-started

You can find her basics about the MVC execution process: http://www.asp.net/mvc/overview/older-versions-1/overview/understanding-the-asp-net-mvc-execution-process

And if you want to dig deeper here a long post from Scott:   🙂 http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx

* Following article helped me with the diagrams, it is a very good introduction as well:
http://www.codeproject.com/Articles/620195/Learning-MVC-Part-Introduction-to-MVC-Architectu

How MVC works – another simple and helpful introduction:
http://www.jquery2dotnet.com/2013/05/how-mvc-works.html

[Updates: made the colors more clear, added list controller example; added two links]