SlideShare uma empresa Scribd logo
1 de 11
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

Mais conteúdo relacionado

Mais procurados

What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.
Confiz
 
Query Management system-Iv review
Query Management system-Iv reviewQuery Management system-Iv review
Query Management system-Iv review
logeshprabu
 
Testclass [Autosaved]
Testclass [Autosaved]Testclass [Autosaved]
Testclass [Autosaved]
Suraj Singh
 
QTP with Descriptive programming
QTP with Descriptive programmingQTP with Descriptive programming
QTP with Descriptive programming
Kuldeep Sharma
 
Android activity intentsq
Android activity intentsqAndroid activity intentsq
Android activity intentsq
perpetrotech
 
Qtp Basics
Qtp BasicsQtp Basics
Qtp Basics
mehramit
 
Less01 1 introduction_module
Less01 1 introduction_moduleLess01 1 introduction_module
Less01 1 introduction_module
Suresh Mishra
 

Mais procurados (19)

What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.What is UFT? HP's unified functional testing.
What is UFT? HP's unified functional testing.
 
Query Management system-Iv review
Query Management system-Iv reviewQuery Management system-Iv review
Query Management system-Iv review
 
Unit testing
Unit testingUnit testing
Unit testing
 
Testclass [Autosaved]
Testclass [Autosaved]Testclass [Autosaved]
Testclass [Autosaved]
 
QTP with Descriptive programming
QTP with Descriptive programmingQTP with Descriptive programming
QTP with Descriptive programming
 
Database Testing
Database TestingDatabase Testing
Database Testing
 
Custom Validation PHP
Custom Validation PHPCustom Validation PHP
Custom Validation PHP
 
Database testing for beginners
Database testing for beginnersDatabase testing for beginners
Database testing for beginners
 
Action filter
Action filterAction filter
Action filter
 
Getting started with Exalate
Getting started with ExalateGetting started with Exalate
Getting started with Exalate
 
Android activity intentsq
Android activity intentsqAndroid activity intentsq
Android activity intentsq
 
Unit testing basics
Unit testing basicsUnit testing basics
Unit testing basics
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testing
 
Basic Database Testing
Basic Database TestingBasic Database Testing
Basic Database Testing
 
Ppt Qtp
Ppt QtpPpt Qtp
Ppt Qtp
 
Qtp Basics
Qtp BasicsQtp Basics
Qtp Basics
 
Filter
FilterFilter
Filter
 
Uft Basics
Uft BasicsUft Basics
Uft Basics
 
Less01 1 introduction_module
Less01 1 introduction_moduleLess01 1 introduction_module
Less01 1 introduction_module
 

Semelhante a ASP.NET MVC controllers

ASP.NET MVC_Routing_Authentication_Aurhorization.pdf
ASP.NET MVC_Routing_Authentication_Aurhorization.pdfASP.NET MVC_Routing_Authentication_Aurhorization.pdf
ASP.NET MVC_Routing_Authentication_Aurhorization.pdf
setit72024
 
Controllers & actions
Controllers & actionsControllers & actions
Controllers & actions
Eyal Vardi
 
important struts interview questions
important struts interview questionsimportant struts interview questions
important struts interview questions
surendray
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
Dilip Patel
 

Semelhante a ASP.NET MVC controllers (20)

.NET Core, ASP.NET Core Course, Session 9
.NET Core, ASP.NET Core Course, Session 9.NET Core, ASP.NET Core Course, Session 9
.NET Core, ASP.NET Core Course, Session 9
 
ASP.MVC Training
ASP.MVC TrainingASP.MVC Training
ASP.MVC Training
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
ASP.NET MVC_Routing_Authentication_Aurhorization.pdf
ASP.NET MVC_Routing_Authentication_Aurhorization.pdfASP.NET MVC_Routing_Authentication_Aurhorization.pdf
ASP.NET MVC_Routing_Authentication_Aurhorization.pdf
 
ASP .net MVC
ASP .net MVCASP .net MVC
ASP .net MVC
 
ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]
 
Controllers & actions
Controllers & actionsControllers & actions
Controllers & actions
 
MVC 4
MVC 4MVC 4
MVC 4
 
ASP.NET MVC 2.0
ASP.NET MVC 2.0ASP.NET MVC 2.0
ASP.NET MVC 2.0
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 
MVC by asp.net development company in india
MVC by asp.net development company in indiaMVC by asp.net development company in india
MVC by asp.net development company in india
 
important struts interview questions
important struts interview questionsimportant struts interview questions
important struts interview questions
 
Overview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company indiaOverview of MVC Framework - by software outsourcing company india
Overview of MVC Framework - by software outsourcing company india
 
Learn Drupal 8 Render Pipeline
Learn Drupal 8 Render PipelineLearn Drupal 8 Render Pipeline
Learn Drupal 8 Render Pipeline
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllers
 
MVC Training Part 1
MVC Training Part 1MVC Training Part 1
MVC Training Part 1
 
Http pipeline
Http pipelineHttp pipeline
Http pipeline
 

Mais de Mahmoud Tolba (10)

The passionate programmer
The passionate programmerThe passionate programmer
The passionate programmer
 
Introduction to SAP, Systems, Applications
Introduction to SAP, Systems, ApplicationsIntroduction to SAP, Systems, Applications
Introduction to SAP, Systems, Applications
 
AngularJS
AngularJSAngularJS
AngularJS
 
Developing cross platforms mobile applications using the Apache Cordova
Developing cross platforms mobile applications using the Apache CordovaDeveloping cross platforms mobile applications using the Apache Cordova
Developing cross platforms mobile applications using the Apache Cordova
 
JQuery Overview
JQuery OverviewJQuery Overview
JQuery Overview
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
 
Windows Communication Foundation
Windows Communication FoundationWindows Communication Foundation
Windows Communication Foundation
 
ASP.NET MVC4 Overview
ASP.NET MVC4 OverviewASP.NET MVC4 Overview
ASP.NET MVC4 Overview
 
Top emerging technologies
Top emerging technologiesTop emerging technologies
Top emerging technologies
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 

Último

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Último (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

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