Professional Documents
Culture Documents
Objectives
Learn how controllers manage MVC applications Understand action methods and how they can receive input Explore how you can return a result from an action method
Agenda
Introduction to Controllers Using a Controller to Manage the Application Controller Actions Returning Action Results
Introduction to Controllers
Controller is the traffic cop to keep things smooth
Responds to user input Manages overall flow of application Interacts with model to change state and data Selects a result to respond to user
Does not itself contain UI, data, or business logic code Ultimately responsible for servicing requests
The applications logic
Controllers in System.Web.Mvc
Controller must:
Implement IController interface Have a name ending in Controller Marked public, not abstract, no generic parameters
Routing Engine
Controller Factory
Controller
View
Action Method
Action Invoker
Response
ActionName Attribute
Normal convention is for controller method name to be same as action name Can give it a different action name
Action name that is not a legal language name Use an MVC component name as action name Different naming standards
ActionMethodSelector Attribute
Now action invoker has list of all matching action names Next examines ActionMethodSelector attributes
Control what conditions a method should be used for a request Single method: IsValidForRequest
o o
Executes on each candidate action method Return false, removed from list
Implementations
NonAction attribute AcceptVerbs attribute HttpDelete, HttpGet, HttpPost, HttpPut
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company
Controller Actions
Once invoked, action method does its job
Receive and process input Perform processing Generate output
o o o o
Select a view Write output directly to page Raw data to browser Do nothing
Parameter Input
Specialized input customized for method MVC takes care of populating values Action invoker examines context objects for parameter values and uses model binder
1. Request.Form 2. RouteData.Values 3. Request.QueryString
Model Bindings
Model encapsulates data and business rules
Use directly in controllers and views Built-in features to make easy Automatic data scaffolding
SessionStateBehavior enumeration
Default Disabled ReadOnly Required
Action Result
ActionResult base class
public abstract class ActionResult { public abstract void ExecuteResult(ControllerContext context); }
ExecuteResult receives context information Takes care of low-level work of generating a response
ActionResult Types
Type
Helper Method
Description
ContentResult
EmptyResult
Content()
--
FileResult
HttpNotFoundResult
File()
HttpNotFound()
HttpStatusCodeResult
HttpUnauthorizedResult JavaScriptResult JsonResult PartialViewResult RedirectResult RedirectToRouteResult ViewResult
Useful to indicate that requested resource is not available Returns HTTP 404 status code
Can mask an exception that an attacker could otherwise use
Redirection Methods
Methods on the Controller class Return instances of redirection action methods
Permanent property set to true
Types
RedirectPermanent method RedirectToRoutePermanent method RedirectToActionPermanent method
Learn More!
This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!