Controllers in Practice

Now that you understand the general function of controllers in ASP.NET, let's take a look at the process for creating one in Visual Studio.

The Controllers folder of a Visual Studio project contains the controller classes used to manage responses and input. Remember that the “Controller” suffix is required.

Begin by creating a file for a controller such as “SuperController.cs.” Next, design code to specify views.

namespace MySoftware.Controllers
	public class CarController : Controller
		// Get all the cars in the garage

		public ActionResult Index(id)
			// Point to the view with content
			return View(cars);


Note that Visual Studio allows code snippets to be saved and automatically generated; for example, on entering a term, a code template for a controller will be created.

ASP.NET MVC maps URLs to action methods within the Controllers class. The Views folder contains the information that defines the action result, which we will examine later. Note that controllers can contain as many methods as necessary to complete a task.


Action methods are usually one-to-one. One-to-one refers to actions like entering a URL, clicking Submit, or clicking a link. They can return virtually any type of data, and though methods are diverse, they must conform to certain rules:

  1. Methods are public.
  2. Methods are not static.
  3. Methods are not extension methods.
  4. Methods are not constructors, getters, or setters.
  5. Methods do not contain open generic types.
  6. Methods are not methods of the controller base class.
  7. Methods do not contain ref or out parameters.

Review an example of an action method below:

public ActionResult Index()
		var SelectedIndex = _rnd.Next(_cars.Count);
		ViewData["Cars"] = _cars[SelectedIndex];
		return View();


ASP.NET web forms provide extension of the event-driven model into web applications causing a browser to send a web form to the server, which then returns a page with full markup. The server processes all client activities. ASP.NET works around HTTP's stateless nature by aiding in storing state information in its runtime.

web hosting


The default procedure for action method parameter values is to get them from data collected by the request. These value types include names or values from forms, query string values, and cookie values. If the value cannot be managed or if its type presents a problem, the passing of null or throwing of an exception occurs. The framework also automatically maps URL values to values for action methods through checking for HTTP request values by default on any incoming request.

There are many ways to access these values. Two means of access include the Request and Response objects of the Controller class. The Request object gathers values passed by the client to the server during an HTTP request. The Response object sends output to the client from the server. Review their syntax, and examples of their use below.

Correct syntax for Request follows:


Correct syntax for Response follows:


Review examples of Request and Response code below:

strEmployeeID = Request.Form( "EmployeeID" )

Response.Write "The number of strTheAmount is " & strTheAmount