LOGIC Library

This site is the Logic involvement in sharing expertise and skills acquired in daily work. The goal is to create a solid knowledge base and share best practices in software development and systems management.

More info about us can be found on logicsistemi.it.

ASP.NET MVC 4 tutorial for web developers - part 4

The Visual Studio template creates some standard views with instructions to start with personalization. In this post we will analyze the controller for the default URL and the view associated with it.

The home controller

If you open the /Controllers/HomeController.cs file you will find the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyTasks.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "xxxxxxxxxxxx";
            return View();
        }
        ... some similar methods for the other views
    }
}

Here you are the things to note:

Class name

The name of the controller is “Home”. The class that define this controller must complete is name with the “Controller” prefix.

Output type of every action is “ActionResult”

Our action must display some HTML code, so the return type could be a simple string. By specifying “ActionResult” .Net will authomatically add some extra code that set additional response headers, such as the content type sent to the browser. To better understand this, try to add the following method to the controller:

public string Test(){
        return "<html><body><p>Hello, world!</p></body></html>";
}

If you call the URL /Home/Test you will see an HTML page like Index, but if you analyze the page information with your browser you will notice that Content-type is not set.

When an action render an HTML page it will be better to use the class ViewResult, derived from ActionResult, but ActionResult is more generic and allows to manage cases where Action lead, for example, to a redirection.

We will see later other result type such as JsonResult.

ViewBag.Message

The ViewBag is an object that can be used to share data between controller and view.

return View()

Is a simple instruction that do all the work! By default this method call the view, in the folder /Views/<controller> with the same name of the action: for us is /Views/Home/Index.cshtml. It's time to open it to understand the additional operations done.

A base view

Opening the view file you will find something that, for a PHP programmer, is very familiar! This strictly resemble a PHP file with a mix of HTML and programming code instruction.

At a first approach the only difference you will note is that code blocks are surrounded by the following syntax:

@{
    ... C# code
}

There are several other differences, but it's not time to talk about them: the name of this technology is Razor and we will see more about it in the next post.

The call to View() function in the controller performs the following actions:

  • loads the specified view or the default one

  • uses the view engine specified in the Web.config file to parse the view file and execute the code in it

  • sets response parameters

  • outputs the HTML (or other) code to the browser

There are 2 things to note in the Index view code.

The first one is the way how the message set in the controller is displayed. You will find in the code the following instruction:

<h2>@ViewBag.Message</h2>

It's something like the PHP code:

<h2><?=ViewBag->Message?></h2>

The ViewBag object, as stated before, is created in the controller and can be accessed by the view.

The second thing to note is that the page is not complete. In the page you will find only the “content” section of the page and the parts that can be considered as a template, such as the header, the menu and the footer, are not present.

This is another feature of the razor engine that will be analyzed in the next post: layouts.

Add comment


Security code
Refresh