By February 26, 2014 0 Comments Read More →

ASP.NET MVC–Key Components And Their Relationships

This post outlines key components of ASP.NET MVC and their connections. This is helpful for those who are getting started with ASP.NET MVC especially to those who are transitioning from ASP.NET Forms.

RouteConfig.cs

RouteConfig.cs file is located in App_Start folder. It defines how to parse and route incoming requests based on the URL’s structure.

By default this is what it has:

url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",

                        action = "Index",

                        id = UrlParameter.Optional });

It means that if you app hosted on myApp.com domain then the request to myApp.com will be routed to Home controller’s Index action by default, same applies if request explicitly requests for it as in myApp.com/Home/Index. The request to myApp.com/Sales/Show/2009 will be routed to Sales controller’s Show action passing 2009 as id parameter.

Controllers

Controllers (C in MVC) responsible for processing routed requests and navigating to resulting views for UI presentation. Controllers are c# files and located in Controllers folder. Naming convention is XxxxController. For example, HomeController, SalesController. Controller’s public methods are actions to which request is routed based on parsed URL as explained in RouteConfig.cs section. For example, request to myApp.com/Sales/Show/2009 will be routed to the following method:

public class SalesController : Controller
  {

      // GET: /Sales/Show/SalesYear
      public ActionResult Show(string id)
      {

           SalesModel sales = GetData(id);

           ….

      }

      return View(sales)

  }

Normally the controller’s method would end up returning data Model to a view to present as a resulting UI, in this case sales.

Views

Views (V in MVC) are markup files that use a mix of c# code and HTML markup, much like in the Interdev and ASP days. These files have cshtml extension. The views files located in Views folder and then inside the folder that’s relevant to it’s controller. For example views related to SalesController would sit inside VIews/Sales folder. Also the name of the view must be the same as the name of the controller’s action. For example Views/Sales/Show.cshtml is the name of the view for Show action of the SlaesController. When controller’s method returns data model to a view the controller will be looking for the view according to this folder structure. When the View executes it assumes data model is passed to it and it can operate on it. For example, the following fragment of cshtml View file assumes SalesModel object was passed to it fron controllers action and it echoes back to the browser the TotalSales property.

@model SalesModel

<span>@Model.TotalSales</span><br />

Models

Models (M in MVC) are data that is being passed around from controller to the view. Controller acts on Model, View present Model in the UI. Models are regular c# classes normally located in Models folder.

Summary

  • It all starts with RouteConfig.cs which defines how to parse incoming requests.
  • Once request is parsed, it’s routed to relevant controller and action inside of it.
  • Normally the action, public method in the controller, would generate data model that’s passed to view for presentation.
  • View grabs the data model and binds to HTML markup that is presented in the browser.
Posted in: Uncategorized

About the Author:

This blog is dedicated to share simple practices I that get me results.

Post a Comment