Understanding ASP.NET MVC Code (For Aspiring Architects) - #3

 Alik Levin    This post is a digest of the Understanding Models, Views, and Controllers (C#). It helps to quickly understand the generated code when creating ASP.NET MVC project in Visual Studio.

Resources 

ASP.NET MVC Project in Visual Studio

  • Three folders created:  Models, Views (ASPX pages sit here), Controllers
  • Urls are SEO friendly (/Home/About)
  • No direct correspondence between URL and the page.

Routing

  • Requests mapped to Controller's actions
  • ASP.NET Web Forms are content centric
  • ASP.NET MVC is logic centric
  • ASP.NET Routing maps request to action.
  • Routing is registered in Global.asax
  • Controllers

    • Controls user interaction (flow) with ASP.NET MVC application.
    • Derive from Controller class.
    • Exposes actions that can return ActionResult.
    • Any public method is action (WARNING: can be invoked freely via URL).
    • A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control).

    Views

    • Create folders by Controllers names.
    • Create sub-folders to reflect views that Controller handles.
    • View is ASPX page that inherits from System.Web.Mvc.ViewPage
    • View should contain only logic related to generating the user interface.

    Models

    • An MVC model contains all of your application logic that is not contained in a view or a controller.

    Related Materials

    This post is made with PracticeThis.com plugin for Windows Live Writer