Dusan Czupek's blog

Blog related .NET, Android & web development

Recent posts








      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      MVC Basics

      The ASP.NET MVC Framework is a web application framework that implements the model-view-controller pattern. Based on ASP.NET, it allows software developers to build a Web application as a composition of three roles: Model, View and Controller. A model represents the state of a particular aspect of the application. Frequently, a model maps to a database table with the entries in the table representing the state of the application. A controller handles interactions and updates the model to reflect a change in state of the application, and then passes information to the view. A view accepts necessary information from the controller and renders a user interface to display that

      - just the UI (HTML), there's no much logic there only logic for taking data from the Model. View just take a Model and render it's content on the page. There's no code behind, no .aspx.vb or .aspx.cs

      Model - contains all data that view needs to display

      Controller - is the boss, the controller handling HTTP requests for web page and it's role is to create a model (read data from DB, bussiness logic etc) and selects proper view for render. Controller never interacts with the user interface, it's job of the View. And never holds any data or statements, it's job of the Model.

      Folder structure of app:

      • "Controllers" folder is the place where you the controllers are placed. The name convention is the controller name must end with "Controller" word, for eg.: HomeController -> Home is the name of the controller. And "Controller" is required postfix. In the code you using just a name (for eg. Home) but controller classes needs the have there the Controller postfix to be properly handled by MVC engine.
      • "Views" is the place for views.
        When the view represents action result (for eg. aspx page) then view name needs to match with action names used for.
        When view is usercontrol you can name it whatever you what.Under Views folder you will have list of folders with controller names and the one called Shared. The folders with the controller names are there just because the view is somehow associated with the controller. Another words when the controller return views that's the place where the MVC is looking for particular view that needs to be renders. The "Shared" folder is the place for user controls, master pages etc. The "Shared" folder contains views used by multiple controllers.
      • "Models" - place for object definitions that will be populated with the Controller and used by View for rendering.

      Routing is the way of directing incoming requests to an MVC controller. In another words map URLS to controller and action with parameters. For example: what controller and action handling default page.
      Routes must be defined during application startup (Application_Start() in global.asax).

      Sample of registration of the route in the global.asax.cs:

          public class MvcApplication : System.Web.HttpApplication
              public static void RegisterRoutes(RouteCollection routes)
                      "Default", // Route name
                      "{controller}/{action}/{id}", // URL with parameters
                      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
              protected void Application_Start()

      This sample handles various types of URL:
      1) if the URL will in format of www.example.com/Something/Something/Something
      2) www.example.com - in this case the for the controller will be used "Home" and action will be "Index" with no additional parameters
      3) www.example.com/home - Home controller and action Index. Index just because it was specified as default paramter (see above)
      4) www.example.com/HoMe/About - Home controller and About action
      5) www.example.com/Process/List - Process is the controller name and List is the action name
      5) www.example.com/Process/List/1 - Process is the controller name, List is the action name, 1 is the ID (value passed to the action)

      Let's say the URL looks like http://www.example.com/Home/About
      Well in this case the "Home" will be the name of controller, "About" will be name of the action.

      Of course you can have multiple routes. Then the first matched route will be invoke.

      Posted: Nov 22 2010, 17:36 by dczupek | Comments (0) RSS comment feed |
      • Currently 0/5 Stars.
      • 1
      • 2
      • 3
      • 4
      • 5
      Filed under: MVC | dotNet

      Pingbacks and trackbacks (2)+

      Comments are closed