SlideShare una empresa de 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

Más contenido relacionado

La actualidad más candente

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 reviewlogeshprabu
 
Unit testing
Unit testingUnit testing
Unit testingjeslie
 
Testclass [Autosaved]
Testclass [Autosaved]Testclass [Autosaved]
Testclass [Autosaved]Suraj Singh
 
QTP with Descriptive programming
QTP with Descriptive programmingQTP with Descriptive programming
QTP with Descriptive programmingKuldeep Sharma
 
Custom Validation PHP
Custom Validation PHPCustom Validation PHP
Custom Validation PHPProdigyView
 
Database testing for beginners
Database testing for beginnersDatabase testing for beginners
Database testing for beginnersSEEDInfotech1
 
Getting started with Exalate
Getting started with ExalateGetting started with Exalate
Getting started with ExalateExalate
 
Android activity intentsq
Android activity intentsqAndroid activity intentsq
Android activity intentsqperpetrotech
 
Introduction to software testing
Introduction to software testingIntroduction to software testing
Introduction to software testingASIT Education
 
Basic Database Testing
Basic Database TestingBasic Database Testing
Basic Database TestingKumar S
 
Qtp Basics
Qtp BasicsQtp Basics
Qtp Basicsmehramit
 
Less01 1 introduction_module
Less01 1 introduction_moduleLess01 1 introduction_module
Less01 1 introduction_moduleSuresh Mishra
 

La actualidad más candente (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
 

Similar a ASP.NET MVC controllers

.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 9aminmesbahi
 
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 desaijinaldesailive
 
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.pdfsetit72024
 
ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]ASP.Net MVC 4 [Part - 2]
ASP.Net MVC 4 [Part - 2]Mohamed Abdeen
 
Controllers & actions
Controllers & actionsControllers & actions
Controllers & actionsEyal Vardi
 
ASP.NET MVC 2.0
ASP.NET MVC 2.0ASP.NET MVC 2.0
ASP.NET MVC 2.0Buu Nguyen
 
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...WebStackAcademy
 
important struts interview questions
important struts interview questionsimportant struts interview questions
important struts interview questionssurendray
 
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 indiaJignesh Aakoliya
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_HourDilip Patel
 
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 controllersMahmoudOHassouna
 
Http pipeline
Http pipelineHttp pipeline
Http pipelinevrluckyin
 

Similar 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
 

Más de Mahmoud Tolba

The passionate programmer
The passionate programmerThe passionate programmer
The passionate programmerMahmoud Tolba
 
Introduction to SAP, Systems, Applications
Introduction to SAP, Systems, ApplicationsIntroduction to SAP, Systems, Applications
Introduction to SAP, Systems, ApplicationsMahmoud Tolba
 
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 CordovaMahmoud Tolba
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity FrameworkMahmoud Tolba
 
Windows Communication Foundation
Windows Communication FoundationWindows Communication Foundation
Windows Communication FoundationMahmoud Tolba
 
ASP.NET MVC4 Overview
ASP.NET MVC4 OverviewASP.NET MVC4 Overview
ASP.NET MVC4 OverviewMahmoud Tolba
 
Top emerging technologies
Top emerging technologiesTop emerging technologies
Top emerging technologiesMahmoud Tolba
 

Más 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

ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Último (20)

ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

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