Home > Articles > Programming > ASP .NET

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Home Controller

Controller classes in AMVC are used to prepare the Model that will be mapped to a view for a particular resource. In this example, AMVC created the following Home Controller that will be used to return the default view for our application:

namespace ASPMvcHello.Controllers
    public class HomeController : Controller
        public ActionResult Index()
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();

        public ActionResult About()
            return View();

You may be wondering what the ActionResult method is for. When the user makes a request to a certain Action of a Controller, that Action's View is returned to the user. For example, if the user types http://www.examplesite.com/home/about, he will be redirected to the view with that name (About.aspx).

You can have more than one result type, as such:

  • ContentResult: Represents a text result.
  • EmptyResult: Represents no result.
  • FileContentResult: Represents a downloadable file (with the binary content).
  • FilePathResult: Represents a downloadable file (with a path).
  • FileStreamResult: Represents a downloadable file (with a file stream).
  • JavaScriptResult: Represents a JavaScript script.
  • JsonResult: Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • PartialViewResult: Represents HTML and markup rendered by a partial view.
  • RedirectResult: Represents a redirection to a new URL.
  • RedirectToRouteResult: Represents a result that performs a redirection by using the specified route values dictionary.
  • ViewResult: Represents HTML and markup rendered by a view.

Models in the MVC architecture are meant to help support some of the business logic that can also technically be performed in the Controllers. They can, more or less, be optional in an application, but are recommended for larger applications. A common practice is to put all your persistence logic in the Model, while the other more abstract business logic is placed in the Controller.

Home View

The home view (as do the other views) inherits the master page for the application. By changing the master page, you change the entire theme of the application. Here is a code snippet of the home view printing out the sample message:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%= Html.Encode(ViewData["Message"]) %></h2>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.

Fairly straightforward. As you may have guessed, the ID of the content tag fills in that part of the master page where that ID is designated. Lastly, the message is displayed using the ViewData dictionary that was assigned the message value in the controller.


This example is only meant to get you familiar with a basic AMVC application. From here, you can experiment by adding other controllers, models, and views. Once you become familiar with using multiple controllers, models, and views, you can then go on to experiment with an ORM (Object Relation Mapping) technology like Hibernate to use in your Model classes. An ORM technology makes working with and managing data in the application easy.

In the next article, I'd like to show you how to integrate an AMVC application with Spring.NET and Nhibernate.

Also, experiment with the AJAX and jQuery libraries for some impressive user interfaces. These libraries are already included in the default application.

  • + Share This
  • 🔖 Save To Your Account