When creating controllers in ASP.NET MVC, the name of the controller classes has the form <ControllerName>Controller. For example, Listing 1 shows the skeleton of the PetController class in the PetController.cs file. The controller name is actually Pet, but the default behavior of the routing engine is to use classes that will have a name ending with the word Controller. This behavior can be modified, but that topic is outside the scope of this book. The class inherits from the Controller base class (in the System.Web.Mvc namespace).
Listing 1. Definition of the Pet Controller Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HaveYouSeenMe.Controllers
{
public class PetController : Controller
{
}
}
The Controller base class gives your classes the functionality to handle requests and the ability to create action methods, which, as described at the beginning of the chapter, are simply regular methods that return an ActionResult object. There are different types of ActionResult objects such as views, files, a redirection to another route, and so forth. Technically, an ActionResult object represents a command that the framework will perform on behalf of the action requested by the user. The ActionResult class is the base class for all the specific implementations of action results. The following classes derive from the ActionResult class and can be used as an action method’s return type:
• ViewResult: Used to return a view to render HTML in the browser. This is the most common ActionResult.
• PartialViewResult: Similar to ViewResult, it returns a partial view.
• ContentResult: Used to return any type of content. By default, it is used to return plain text, but the actual content type can be explicitly defined (e.g. text/xml).
• EmptyResult: This is the equivalent of a void method—it is by definition an action result object, but it does nothing.
• FileResult: Used to return binary content (e.g., if you want to download a file).
• HttpUnauthorizedResult: You can return an HttpUnauthorizedResult object when the request tries to access restricted content that, for example, is not available to anonymous users. The browser is then redirected to the login page.
• JavaScriptResult: Used to return JavaScript code.
• JsonResult: Used to return an object in JavaScript Object Notation (JSON) format.
• RedirectResult: Used to perform an HTTP redirect to another URL. You can define it as a temporary (code 302) or permanent (code 301) HTTP redirect.
• RedirectToRouteResult: used to perform an HTTP redirect, but to a specific route rather than a URL.
Listing 1. Definition of the Pet Controller Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HaveYouSeenMe.Controllers
{
public class PetController : Controller
{
}
}
The Controller base class gives your classes the functionality to handle requests and the ability to create action methods, which, as described at the beginning of the chapter, are simply regular methods that return an ActionResult object. There are different types of ActionResult objects such as views, files, a redirection to another route, and so forth. Technically, an ActionResult object represents a command that the framework will perform on behalf of the action requested by the user. The ActionResult class is the base class for all the specific implementations of action results. The following classes derive from the ActionResult class and can be used as an action method’s return type:
• ViewResult: Used to return a view to render HTML in the browser. This is the most common ActionResult.
• PartialViewResult: Similar to ViewResult, it returns a partial view.
• ContentResult: Used to return any type of content. By default, it is used to return plain text, but the actual content type can be explicitly defined (e.g. text/xml).
• EmptyResult: This is the equivalent of a void method—it is by definition an action result object, but it does nothing.
• FileResult: Used to return binary content (e.g., if you want to download a file).
• HttpUnauthorizedResult: You can return an HttpUnauthorizedResult object when the request tries to access restricted content that, for example, is not available to anonymous users. The browser is then redirected to the login page.
• JavaScriptResult: Used to return JavaScript code.
• JsonResult: Used to return an object in JavaScript Object Notation (JSON) format.
• RedirectResult: Used to perform an HTTP redirect to another URL. You can define it as a temporary (code 302) or permanent (code 301) HTTP redirect.
• RedirectToRouteResult: used to perform an HTTP redirect, but to a specific route rather than a URL.
No comments:
Post a Comment