Tuesday, November 10, 2009

Understanding MVC Controllers

In a previous post when took an overview of the Hello World application and located where are the major three elements: Models, Views and Controllers.
Now we're going to explore the MVC Controller in details.
We saw that Controllers respond to browser requests, the controller might return a specific view or redirects to another controller
The following code demonstrates a Controller class for a view called Demo



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Mvc.Ajax;



namespace HelloWorld.Controllers

{

public class DemoController : Controller

{

//

// GET: /Demo/



public ActionResult Index()

{

return View();

}

public ActionResult Page2()

{

return View();

}



}

}





This class has two functions: Index() which returns Index.aspx (located in Views/Demo/Index.aspx) when the user types

[URL]/Demo in the browser.

The second function is Page2() which returns Page2.aspx (located in Views/Demo/Page2.aspx) when the user types [URL]/Demo/Page2 in the browser



The controller exposes Controller Actions, an action is a function on a controller that is invoked when a certain url is entered in the browser.

so when the user types [URL]/Demo/Page2, Page2() function is a controller action on DemoController.cs

The Controller Action must be:


  1. Public: Any function declared as public in a Controller is exposed as Controller Action automatically. So be careful when declaring public methods as they are accessible by the user if the correct URL is typed.

  2. Controller Action can not be overloaded.

  3. Controller Action can not be static.
We saw that the Controller Action returns an object of type ActionResult, so what is the
ActionResul tobject:
The ActionResult can be one of these
  1. ViewResult – Represents HTML and markup.
  2. EmptyResult – Represents no result.
  3. RedirectResult – Represents a redirection to a new URL.
  4. JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  5. JavaScriptResult – Represents a JavaScript script.
  6. ContentResult – Represents a text result.
  7. FileContentResult – Represents a downloadable file (with the binary content).
  8. FilePathResult – Represents a downloadable file (with a path).
  9. FileStreamResult – Represents a downloadable file (with a file stream).

All these ActionResults inherit from ActionResult base class.

In our Index() Method we saw that it returns a Viewresult by calling the View() method.
Normally you don't return an ActionResult directly instead you call one of these methods of the Controller base class
  1. View – Returns a ViewResult action result.
  2. Redirect – Returns a RedirectResult action result.
  3. RedirectToAction – Returns a RedirectToRouteResult action result.
  4. RedirectToRoute – Returns a RedirectToRouteResult action result.
  5. Json – Returns a JsonResult action result.
  6. JavaScriptResult – Returns a JavaScriptResult.
  7. Content – Returns a ContentResult action result.
  8. File – Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

Consider the following functions



public string Index()
{
return "Hello World";
}
public DateTime Page2()
{
return DateTime.Now;
}

The Index method just returns a string "Hello World"
The Page2() method retuns the current date and time as a plain text

No comments:

Post a Comment