ASP.NET MVC Terminology

If you are just starting out with ASP.NET MVC, I thought this would be a good resource by presenting this dictionary to assist my readers in understanding everything about ASP.NET MVC.

Written by Jonathan "JD" Danylko • Last Updated: • MVC •

Dictionary with Magnifying Glass

When I started learning ASP.NET MVC eight years ago, some of the terms eluded me.

At the time, there really wasn't a simple definition or list of terms used in the new MVC world.

I thought why not create an ASP.NET MVC Dictionary of terms for people interested in learning MVC.

If I'm missing any terms, please let me know in the comments and I'll add them accordingly.

ActionFilter

Another name for Data Annotations, or attributes, placed on controllers or actions. ActionFilters are a way to control the behavior of an action before or after it's executed.

This could include redirecting unauthorized visitors to a different page or modify HTML before it's returned to the browser.

A common example of an ActionFilter is the AuthorizeAttribute show below. You can also create custom Action Filters.

Example - On an action

public class HomeController : Controller
{
    [Authorize]
    public ActionResult Index()
    {
        return View();
    }
}

Example - On a controller

[Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    } 
}

ActionResults

Crazy as it sounds, ActionResults are results of an action method in the controller. Out of the box, ASP.NET MVC comes with the following ActionResults:

  • ContentResult - Returns custom content
  • EmptyResult - Returns nothing from the action
  • FileResult - Sends a file in binary format
  • HttpStatusCodeResult - Returns a Http Status Code (like a 200 or 404) and a Description
  • JavaScriptResult - Sends JavaScript back from the Action
  • JsonResult - Returns a serialized object in JSON format
  • RedirectResult - Redirects users to another page
  • RedirectToRouteResult - Redirects users to another page using a pre-defined route
  • ViewResultBase - Return data (or ViewModel) to a specific view. If no view is specified, the data will be sent to action method's name in the folder named after the controller under the Views directory (/Views/{controller}/{action}.cshtml).

Example - On an Index Action Method. The statement "return View()" may look strange, but the View() method is a type of ViewResultBase making it easier to call that instead of new-ing up an instance.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Actions

Actions are methods inside the controller. Most of the time, each action represents a View or PartialView located in the Views folder under the appropriate controller's folder name.

Example - The Index method is considered an Action method of the Home Controller.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Areas

If you need more "url-space," you my want to look into Areas. Areas allow you to partition your web application further by "prefixing" a folder onto your existing website URL structure.

If you right-click your project in Visual Studio, an option to create an area will appear. Once you enter your new area name, a folder named Areas will be created with a new folder dedicated to your Area.

This new area holds another "sub-website" with the same folder structure as your root. This enabled you to create new controllers/models/views specific to only that area.

If you have a URL like this,

http://www.domain.com/Blog/Record/5

your route would look like this:

{controller}/{action}/{id}

What if you want to add a store to your site and want it to have it's own blog? You have a little bit of a problem with your site structure.

Add an area called "store" or "shop" and this partitions your website further.

Now you can have a URL that looks like

http://www.domain.com/Shop/Blog/Record/5

Based on the routing engine, this will send the user to the Shop area, Blog Controller in the Shop area, execute the Record Action Method, and send it record number 5.

This is more of a structural decision for a webmaster to decide whether to use areas or not.

Controllers

The (C) in MVC. Controllers are considered to be the coordinators or heavy lifters of your application. They orchestrate the retrieving and saving of data, populating models, and receive input from users.

Example - A home controller with a simple Action method called Index.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Display Modes

Display modes are used to return different Views based on a device, or Display Mode.

For example, if you add an iOS Display Mode, your browser will detect that device and you can have two Views: one for standard display (/Views/Home/Index.cshtml) and one for iOS (/Views/Home/Index.iOS.cshtml).

There is one downside to using this method as I mentioned in my post called Creating Google AMP Pages With ASP.NET MVC.

Html Helpers

Html Helpers is code to assist with creating programmatic HTML. A general rule for Views is to avoid if..then statements. This should be a hint to create an HtmlHelper.

Html Helpers (along with Url Helpers) are merely extension methods and are very easy to create.

An example of an HtmlHelper could be to display a notification to the user.

More common HtmlHelpers are the Html.ActionLink or Html.Partial().

Model Binders

When a user posts data through a submit button, it is sent to a default model binder to match all of the form's variables into a ViewModel of some kind.

This ViewModel is then passed to the controller for the developer to use as they see fit.

Even though the Default Model Binder is hidden, you can create your own custom model binders for any type of input.

Also, make sure you unit test your ModelBinders.

Models

The (M) in MVC. Models hold the data for your application and are considered the business logic of your application.

An example would be this Location model.

public class Location
{
    public Location() { }
    public int LocationId { getset; }
    [Required]
    [StringLength(200)]
    public string Address { getset; }
    [Required]
    [StringLength(100)]
    public string City { getset; }
    [Required]
    [StringLength(10)]
    public string State { getset; }
    [Required]
    [StringLength(15)]
    public string PostalCode { getset; }
    [Required]
    [StringLength(50)]
    public string CountryID { getset; }
}

This model has Data Annotations on each property making it easier to validate the object with user data.

Partial View

Partial views are the children to Views.

Partial Views are stored in the /Views/Shared folder and can be used in the following manner.

@Html.Partial("MenuSystem")

If you have a ViewModel containing a list of models, you can easily pass models into Partial Views as well.

@Html.Partial("MenuSystem", Model.ListOfMenuItems)

Think of these as common HTML snippets used throughout your application to avoid duplication of code.

Razor View Engine

The Razor View Engine is a more modern way of templating models into your application's Views.

To use variables in your code, use a single at symbol (@).

For example, if you want to write a copyright with a date range, you could write:

<p>&copy; Copyright 2006-@DateTime.Now.Year</p>

If you want to execute a block of code, you use the at symbol with curly brackets.

@{
    var greeting = "Hi there, ";
    var name = "Jonathan";
    var fullGreeting = greeting + name;
}
@fullGreeting

You may want to stay away from this type of coding and just use an HtmlHelper. You don't want to dirty up your Views too much.

Quick Tip: What if you need to display an At symbol in your HTML? Use a double at symbol (@@).

Routing

Routing are how requests are processed and are considered the traffic cop of ASP.NET MVC.

As requests come in, they are compared to the route table in your App_Start/RouteConfig.cs and then directed to the appropriate controller.

Quick Tip: If you want to change the default page when running for the first time, change the controller and action in the default route.

Example - the default route for an MVC application in the App_Start/RouteConfig.cs

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

TempData

TempData is a one-shot deal and is only used (as the name implies) temporarily.

If you wanted to display a message to the user, this would be a great place to put the message. The next time a request is made, the TempData cleans up after itself. It's cleared out.

There are a number of other ways to pass data to a View, but I would recommend using ViewModels.

Url Helpers

Url Helpers is the cousin to HtmlHelpers. Instead of creating HTML, they create Urls for the application.

Url Helpers utilize the routing of MVC to create the Urls as a relative path.

One great way to use UrlHelpers is to create a folder and place all URLs in that folder.

Example - A UrlHelper to return the url of the root application

public static string RootUrl(this UrlHelper helper)
{
    return helper.Content("~");
}

In your View, now you can easily use Razor syntax with strong-typing your Urls.

<a href="@Url.RootUrl()">Main Site</a>

ViewBag

ViewBag is another way to pass data to the View, but again, it's not optimal. In your View, you need to cast it to whatever type you want and it muddies the View with code.

You want your Views to be a simple template, plugging in values as you go.

Again, I refer you to use other ways to pass data to a View.

ViewData

Yet, another way to pass data to a View. Better than a ViewBag, but not as versatile as ViewModels. Again, I refer you on how to pass data to a View.

Views

The (V) in MVC. Simply put, Views are the HTML files located in the /Views directory. Folders under the Views directory should all be named after the controllers in your Controllers directory in the root of your application.

There is also a Shared directory holding common HTML snippets. 

ViewModels

ViewModels are considered the easiest way to pass data into Views. These are merely complex or compound objects comprised of one or many models.

Example - this ViewModel contains a Location model (from above) and a User model.

public class LocationViewModel
{
    public Location Location { getset; }
    public User User { getset; }
}

Once this ViewModel is populated through the controller, it's passed on to the Index View.

public ActionResult Index()
{
    var model = new LocationViewModel
    {
        Location = new Location
        {
            City = "Columbus",
            State = "OH"
        },
        User = new User
        {
            Name = "Jonathan Danylko"
        }
    };
    return View(model); }

Finally, again, I refer you on how to pass data to a View.

Conclusion

Today, I went back to the basics to help my readers understand the terminology of an ASP.NET MVC application.

For further reading, I recommend the following:

If you have any questions about additional terms, please post a comment below and I'll update this living document as more terms appear.

Did you like this content? Show your support by buying me a coffee.

Buy me a coffee  Buy me a coffee
Picture of Jonathan "JD" Danylko

Jonathan Danylko is a web architect and entrepreneur who's been programming for over 25 years. He's developed websites for small, medium, and Fortune 500 companies since 1996.

He currently works at Insight Enterprises as an Principal Software Engineer Architect.

When asked what he likes to do in his spare time, he replies, "I like to write and I like to code. I also like to write about code."

comments powered by Disqus