Thursday, November 25, 2010

MVC Controller


MVC controllers are responsible for handling incoming HTTP requests, handling user input, retrieving and saving data, and determining the response to send back to the client.

Each browser request is mapped to a particular controller.

Controllers are derived from System.Web.Mvc.Controller (built-in controller base class).

Action

Each public method on a controller class is an action method, which means you can invoke it from the web via some URL.

A method used as a controller action cannot be overloaded.
The method must be public.
The method cannot be a static or extension method.
The method cannot be a constructor, getter, or setter.
The method cannot have open generic types.
The method is not a method of the controller base class.
The method cannot contain ref or out parameters.

A controller action can be invoked by anyone connected to the Internet.
If you don’t want to expose the method as a controller action then you can prevent the method from being invoked by using the [NonAction] attribute.

A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

The ASP.NET MVC framework supports several types of action results including:
Action Result
Description
Controller base class method
ViewResult
Represents HTML and markup
View
EmptyResult
Represents no result

RedirectResult
Represents a redirection to a new URL
Redirect
JsonResult
Represents a JavaScript Object Notation result that can be used in an AJAX application
Json
JavaScriptResult
Represents a JavaScript script
JavaScriptResult
ContentResult
Represents a text result
Content
FileContentResult
Represents a downloadable file (with the binary content)
File
FilePathResult
Represents a downloadable file (with a path)
File
FileStreamResult
Represents a downloadable file (with a file stream)
File


No comments:

Post a Comment