ASP.NET MVC
Controllers
Controllers process incoming requests, handle user input and interactions, and execute appropriate application...
Action methods
 Action methods map to user interactions
 Examples of user interactions include
• Entering a URL
• Submit...
Action methods (Cont.)
 The method must be public.
 The method cannot be a static method.
 The method cannot be an exte...
Preventing a Public Method from Being Invoked
Action methods Return Types
Most action methods return an instance of a class that derives from ActionResult
ViewResult

R...
Action Methods Parameters
The values for action method parameters are retrieved from the request's data collection.
The da...
Action Filters
An action filter is an attribute that you can apply to a controller action -- or an entire controller -- th...
Action Filter Types
 Authorization filters – Implements the IAuthorizationFilter attribute.
 Action filters – Implements...
Implementing Custom Action Filters
Custom Action filters inherit from ActionFilterAttribute class and has the following me...
Action Selectors
ASP.NET MVC 3 defines a set of Action selectors which determine the selection of an Action. One of them i...
Upcoming SlideShare
Loading in …5
×

ASP.NET MVC controllers

2,116 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,116
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ASP.NET MVC controllers

  1. 1. ASP.NET MVC
  2. 2. Controllers Controllers process incoming requests, handle user input and interactions, and execute appropriate application logic. A controller class typically calls a separate view component to generate the HTML markup for the request  Controllers inherit from Controller class  User interactions are translated to action methods  All controllers must end with Controller suffix  Controller responsibilities 1. Locating the appropriate action method to call and validating that it can be called. 2. Getting the values to use as the action method's arguments. 3. Handling all errors that might occur during the execution of the action method. 4. Providing the default WebFormViewEngine class for rendering ASP.NET page types (views).
  3. 3. Action methods  Action methods map to user interactions  Examples of user interactions include • Entering a URL • Submitting a form • Clicking a link
  4. 4. Action methods (Cont.)  The method must be public.  The method cannot be a static method.  The method cannot be an 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.
  5. 5. Preventing a Public Method from Being Invoked
  6. 6. Action methods Return Types Most action methods return an instance of a class that derives from ActionResult ViewResult Renders a view as a Web page. PartialViewResult Renders a partial view, which defines a section of a view that can be rendered inside another view. RedirectResult Redirects to another action method by using its URL. RedirectToRouteResult Redirects to another action method. ContentResult Returns a user-defined content type. JsonResult Returns a serialized JSON object. JavaScriptResult Returns a script that can be executed on the client. FileResult Returns binary output to write to the response. EmptyResult
  7. 7. Action Methods Parameters The values for action method parameters are retrieved from the request's data collection. The data collection includes name/values pairs for form data, query string values, and cookie values. The ASP.NET MVC framework can automatically map URL parameter values to parameter values for action methods. By default, if an action method takes a parameter, the MVC framework examines incoming request data and determines whether the request contains an HTTP request value with the same name. If so, the request value is automatically passed to the action method
  8. 8. Action Filters An action filter is an attribute that you can apply to a controller action -- or an entire controller -- that modifies the way in which the action is executed Action Filters in the ASP.NET MVC framework  OutputCache – This action filter caches the output of a controller action for a specified amount of time.  HandleError – This action filter handles errors raised when a controller action executes.  Authorize – This action filter enables you to restrict access to a particular user or role.  You can develop your custom filters.
  9. 9. Action Filter Types  Authorization filters – Implements the IAuthorizationFilter attribute.  Action filters – Implements the IActionFilter attribute.  Result filters – Implements the IResultFilter attribute.  Exception filters – Implements the IExceptionFilter attribute.
  10. 10. Implementing Custom Action Filters Custom Action filters inherit from ActionFilterAttribute class and has the following methods OnActionExecuting – This method is called before a controller action is executed. OnActionExecuted – This method is called after a controller action is executed. OnResultExecuting – This method is called before a controller action result is executed. OnResultExecuted – This method is called after a controller action result is executed.
  11. 11. Action Selectors ASP.NET MVC 3 defines a set of Action selectors which determine the selection of an Action. One of them is ActionName, used for defining an alias for an Action. When we define an alias for an Action, the Action will be invoked using only the alias; not with the Action name. Selector Types  HttpGet  HttpPost

×