SlideShare a Scribd company logo
ASP.NET MVC
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).
Action methods
 Action methods map to user interactions
 Examples of user interactions include
• Entering a URL
• Submitting a form
• Clicking a link
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.
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

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
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
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.
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.
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.
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

More Related Content

PPSX
Software testing and_quality_assurance_powerpoint_presentation
DOCX
Step types
PPTX
Java Unit Test - JUnit
PDF
Unit test documentation
PPT
Automation tips
PDF
Query Management system-Iv review
PPTX
Software Evaluation
PPTX
Coded ui - lesson 5 - ui map
Software testing and_quality_assurance_powerpoint_presentation
Step types
Java Unit Test - JUnit
Unit test documentation
Automation tips
Query Management system-Iv review
Software Evaluation
Coded ui - lesson 5 - ui map

What's hot (19)

PDF
What is UFT? HP's unified functional testing.
ODP
Query Management system-Iv review
PPT
Unit testing
PPTX
Testclass [Autosaved]
PPT
QTP with Descriptive programming
PPS
Database Testing
PPTX
Custom Validation PHP
PDF
Database testing for beginners
PPTX
Action filter
PPTX
Getting started with Exalate
PPTX
Android activity intentsq
PPT
Introduction to software testing
PPT
Basic Database Testing
PPT
Ppt Qtp
PPT
Qtp Basics
PPTX
Filter
PPTX
Uft Basics
PPT
Less01 1 introduction_module
What is UFT? HP's unified functional testing.
Query Management system-Iv review
Unit testing
Testclass [Autosaved]
QTP with Descriptive programming
Database Testing
Custom Validation PHP
Database testing for beginners
Action filter
Getting started with Exalate
Android activity intentsq
Introduction to software testing
Basic Database Testing
Ppt Qtp
Qtp Basics
Filter
Uft Basics
Less01 1 introduction_module
Ad

Similar to ASP.NET MVC controllers (20)

PDF
.NET Core, ASP.NET Core Course, Session 9
PPTX
ASP.MVC Training
PDF
Mvc interview questions – deep dive jinal desai
PPTX
Chapter4.pptx
PPTX
Asp.net mvc
PDF
ASP.NET MVC_Routing_Authentication_Aurhorization.pdf
PPT
ASP .net MVC
PPTX
ASP.Net MVC 4 [Part - 2]
PPTX
Controllers & actions
PPTX
PPTX
Model View Controller-Introduction-Overview.pptx
PDF
ASP.NET MVC 2.0
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
PPTX
MVC by asp.net development company in india
PDF
important struts interview questions
PPTX
Overview of MVC Framework - by software outsourcing company india
PDF
Learn Drupal 8 Render Pipeline
PPTX
MVC & SQL_In_1_Hour
PDF
Murach: How to transfer data from controllers
PPTX
MVC Training Part 1
.NET Core, ASP.NET Core Course, Session 9
ASP.MVC Training
Mvc interview questions – deep dive jinal desai
Chapter4.pptx
Asp.net mvc
ASP.NET MVC_Routing_Authentication_Aurhorization.pdf
ASP .net MVC
ASP.Net MVC 4 [Part - 2]
Controllers & actions
Model View Controller-Introduction-Overview.pptx
ASP.NET MVC 2.0
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
MVC by asp.net development company in india
important struts interview questions
Overview of MVC Framework - by software outsourcing company india
Learn Drupal 8 Render Pipeline
MVC & SQL_In_1_Hour
Murach: How to transfer data from controllers
MVC Training Part 1
Ad

More from Mahmoud Tolba (10)

PPTX
The passionate programmer
PPTX
Introduction to SAP, Systems, Applications
PPTX
AngularJS
PPTX
Developing cross platforms mobile applications using the Apache Cordova
PPTX
JQuery Overview
PPTX
Microsoft Entity Framework
PPTX
Windows Communication Foundation
PPTX
ASP.NET MVC4 Overview
PPTX
Top emerging technologies
PPTX
Advanced JavaScript
The passionate programmer
Introduction to SAP, Systems, Applications
AngularJS
Developing cross platforms mobile applications using the Apache Cordova
JQuery Overview
Microsoft Entity Framework
Windows Communication Foundation
ASP.NET MVC4 Overview
Top emerging technologies
Advanced JavaScript

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Lesson notes of climatology university.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Institutional Correction lecture only . . .
PDF
Insiders guide to clinical Medicine.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Classroom Observation Tools for Teachers
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Pre independence Education in Inndia.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Types and Its function , kingdom of life
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Sports Quiz easy sports quiz sports quiz
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Lesson notes of climatology university.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Institutional Correction lecture only . . .
Insiders guide to clinical Medicine.pdf
Computing-Curriculum for Schools in Ghana
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
GDM (1) (1).pptx small presentation for students
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
Pre independence Education in Inndia.pdf
Renaissance Architecture: A Journey from Faith to Humanism
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
Cell Types and Its function , kingdom of life
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

ASP.NET MVC controllers

  • 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. Action methods  Action methods map to user interactions  Examples of user interactions include • Entering a URL • Submitting a form • Clicking a link
  • 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. Preventing a Public Method from Being Invoked
  • 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. 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. 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. 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. 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. 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