SlideShare a Scribd company logo
Controllers & Actions in MVCEyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com
AgendaControllers OverviewViewData vs. ViewBagAction & ActionResultController AttributesAsync Controller
Understanding ControllersMVC controllers are responsible for responding to requests made against an ASP.NET MVC website.
The Controller  ResponsibilityExecute the right action method and validating that it can be called.Getting the values to use as the action method's arguments.Handling all errors that might occur during the execution of the action method.Providing the default WebFormViewEngineclass for rendering ASP.NET page types (views).
Controller Samplehttp://E4D.co.il / Course / Net / MVC /http://E4D.co.il / Course/ Net ? name=MVCpublic class CourseController : Controller{   public ActionResult Net( string name )   {       var course = BL.GetCourse(name);       return View( course );   }}
ViewData PropertyUsed to set view-specific data in a dictionary object that can hold multiple name/value pairs.public class CourseController : Controller{   public ActionResult Net( string name )   {       ViewData["Course"] = BL.GetCourse(name);       return View();   }}
ViewBag PropertyUsed to set view-specific data in a dictionary object that can hold multiple name/value pairs.public class CourseController : Controller{   public ActionResult Net( string name )   {       ViewBag.Course = BL.GetCourse(name);       return View();   }}
ActionsAction == MethodAction must meet certain requirements:Must be public.Cannot be a static method.Cannot be an extension 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 an Action to InvokePrevent the method from being invoked by using the [NonAction] attribute. public class HomeController : Controller{    [NonAction]    public string CompanySecrets()    {        return "This information is secret.";    }    ...}
Action ResultsA controller action returns something called an action result.Action results types:ViewResultEmptyResultRedirectResultJsonResultJavaScriptResultContentResultFileContentResultFilePathResultFileStreamResultHttpNotFoundResultHttpRedirectResultHttpStatusCodeResult
Controller “View” MethodsNormally, you do not return an action result directly. Instead, you call one of the following methods of the Controller base class.
Controller “View”Methods
Controller Class
Controller Attributes[ChildActionOnly][SessionState][OutputCache][ActionName][NonAction][AcceptVerbs][HttpGet][HttpPost][HttpPut][HttpDelete]
SessionState AttributeUsing the attribute, you can completely turn on or off session state, adjust it to read-only, or make it required.Default,  Required,  ReadOnly & Disabled.[SessionState( SessionStateBehavior.Disabled )]public class HomeController : Controller     {   public ActionResult Index()   {       Session["E4D"] = "E4D Learning";                   return View();   }     }
OutputCacheAttribute[ChildActionOnly][OutputCache( Duration = 20 )]public ActionResult CurrentTime()         {    return PartialView();         }<h2>OutputCache Sample</h2> View Time: @DateTime.Now.ToString("h:mm:ss")<br /> Partial View Time: @{    Html.RenderAction("CurrentTime");}
Async ControllerThe AsyncControllerclass enables you to write asynchronous action methods.
Async Actionpublic class PortalController : AsyncController{     [AsyncTimeout(Duration = 5000 )]    publicvoidNewsAsync(string city)     { AsyncManager.OutstandingOperations.Increment(); NewsServicenewsService = newNewsService(); newsService.GetHeadlinesCompleted+= (sender, e) =>         { AsyncManager.Parameters["headlines"] = e.Value; AsyncManager.OutstandingOperations.Decrement();         }; newsService.GetHeadlinesAsync(city);     }      public ActionResult NewsCompleted(NewsHeadline[] headlines)     {    return View( "News",                  new ViewStringModel { Headlines = headlines } );     }}Async Action
Async Action

More Related Content

What's hot (20)

PPTX
React with Redux
Stanimir Todorov
 
PDF
React js t7 - forms-events
Jainul Musani
 
PDF
React JS and Redux
Glib Kechyn
 
PPTX
Introduction to React JS for beginners
Varun Raj
 
PPTX
React outbox
Angela Lehru
 
PPTX
Redux training
dasersoft
 
PDF
Intro to Redux | DreamLab Academy #3
DreamLab
 
PPTX
React & redux
Cédric Hartland
 
PDF
React redux
Michel Perez
 
PPTX
Redux workshop
Imran Sayed
 
PPTX
Introduction to react and redux
Cuong Ho
 
PDF
React and redux
Mystic Coders, LLC
 
PDF
Switch to React.js from AngularJS developer
Eugene Zharkov
 
PDF
React, Redux, ES2015 by Max Petruck
Maksym Petruk
 
PPTX
React + Redux + TypeScript === ♥
Remo Jansen
 
PDF
Redux vs Alt
Uldis Sturms
 
PDF
React for Dummies
Mitch Chen
 
PPTX
React + Redux Introduction
Nikolaus Graf
 
PDF
Intro to ReactJS
Harvard Web Working Group
 
PPTX
Intro to React
Eric Westfall
 
React with Redux
Stanimir Todorov
 
React js t7 - forms-events
Jainul Musani
 
React JS and Redux
Glib Kechyn
 
Introduction to React JS for beginners
Varun Raj
 
React outbox
Angela Lehru
 
Redux training
dasersoft
 
Intro to Redux | DreamLab Academy #3
DreamLab
 
React & redux
Cédric Hartland
 
React redux
Michel Perez
 
Redux workshop
Imran Sayed
 
Introduction to react and redux
Cuong Ho
 
React and redux
Mystic Coders, LLC
 
Switch to React.js from AngularJS developer
Eugene Zharkov
 
React, Redux, ES2015 by Max Petruck
Maksym Petruk
 
React + Redux + TypeScript === ♥
Remo Jansen
 
Redux vs Alt
Uldis Sturms
 
React for Dummies
Mitch Chen
 
React + Redux Introduction
Nikolaus Graf
 
Intro to ReactJS
Harvard Web Working Group
 
Intro to React
Eric Westfall
 

Similar to Controllers & actions (20)

PPTX
Asp.net mvc
erdemergin
 
PPTX
ASP.NET MVC controllers
Mahmoud Tolba
 
PPTX
ASP.MVC Training
Mahesh Sikakolli
 
PPTX
Chapter4.pptx
narendrakumar406336
 
PPTX
Developing ASP.NET Applications Using the Model View Controller Pattern
goodfriday
 
PDF
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
PPTX
Asp.Net MVC Intro
Stefano Paluello
 
PDF
Asp.Net MVC Framework Design Pattern
maddinapudi
 
PPTX
MVC Training Part 1
Lee Englestone
 
PDF
.NET Core, ASP.NET Core Course, Session 9
Amin Mesbahi
 
PPT
ASP .net MVC
Divya Sharma
 
PPTX
Asp.net mvc presentation by Nitin Sawant
Nitin S
 
PDF
Murach: How to transfer data from controllers
MahmoudOHassouna
 
PPS
Introduction To Mvc
Volkan Uzun
 
PPTX
Mvc ppt
Gowarthini
 
PPT
Sessi
Asif Atick
 
PPT
Inside asp.net mvc framework
Ciklum Ukraine
 
PPT
Inside ASP.NET MVC framework
Ciklum Ukraine
 
PPTX
MVC & SQL_In_1_Hour
Dilip Patel
 
PPT
ASP.NET-MVC-Part-1.ppt
Saraswathi Ramesh
 
Asp.net mvc
erdemergin
 
ASP.NET MVC controllers
Mahmoud Tolba
 
ASP.MVC Training
Mahesh Sikakolli
 
Chapter4.pptx
narendrakumar406336
 
Developing ASP.NET Applications Using the Model View Controller Pattern
goodfriday
 
Mvc interview questions – deep dive jinal desai
jinaldesailive
 
Asp.Net MVC Intro
Stefano Paluello
 
Asp.Net MVC Framework Design Pattern
maddinapudi
 
MVC Training Part 1
Lee Englestone
 
.NET Core, ASP.NET Core Course, Session 9
Amin Mesbahi
 
ASP .net MVC
Divya Sharma
 
Asp.net mvc presentation by Nitin Sawant
Nitin S
 
Murach: How to transfer data from controllers
MahmoudOHassouna
 
Introduction To Mvc
Volkan Uzun
 
Mvc ppt
Gowarthini
 
Sessi
Asif Atick
 
Inside asp.net mvc framework
Ciklum Ukraine
 
Inside ASP.NET MVC framework
Ciklum Ukraine
 
MVC & SQL_In_1_Hour
Dilip Patel
 
ASP.NET-MVC-Part-1.ppt
Saraswathi Ramesh
 
Ad

More from Eyal Vardi (20)

PPTX
Why magic
Eyal Vardi
 
PPTX
Smart Contract
Eyal Vardi
 
PDF
Rachel's grandmother's recipes
Eyal Vardi
 
PPTX
Performance Optimization In Angular 2
Eyal Vardi
 
PPTX
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
PPTX
Angular 2 NgModule
Eyal Vardi
 
PPTX
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
PPTX
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
PPTX
Routing And Navigation
Eyal Vardi
 
PPTX
Angular 2 Architecture
Eyal Vardi
 
PPTX
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
PPTX
Angular 2.0 Views
Eyal Vardi
 
PPTX
Component lifecycle hooks in Angular 2.0
Eyal Vardi
 
PPTX
Template syntax in Angular 2.0
Eyal Vardi
 
PPTX
Http Communication in Angular 2.0
Eyal Vardi
 
PPTX
Angular 2.0 Dependency injection
Eyal Vardi
 
PPTX
Angular 2.0 Routing and Navigation
Eyal Vardi
 
PPTX
Async & Parallel in JavaScript
Eyal Vardi
 
PPTX
Angular 2.0 Pipes
Eyal Vardi
 
PPTX
Angular 2.0 forms
Eyal Vardi
 
Why magic
Eyal Vardi
 
Smart Contract
Eyal Vardi
 
Rachel's grandmother's recipes
Eyal Vardi
 
Performance Optimization In Angular 2
Eyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Eyal Vardi
 
Angular 2 NgModule
Eyal Vardi
 
Upgrading from Angular 1.x to Angular 2.x
Eyal Vardi
 
Angular 2 - Ahead of-time Compilation
Eyal Vardi
 
Routing And Navigation
Eyal Vardi
 
Angular 2 Architecture
Eyal Vardi
 
Angular 1.x vs. Angular 2.x
Eyal Vardi
 
Angular 2.0 Views
Eyal Vardi
 
Component lifecycle hooks in Angular 2.0
Eyal Vardi
 
Template syntax in Angular 2.0
Eyal Vardi
 
Http Communication in Angular 2.0
Eyal Vardi
 
Angular 2.0 Dependency injection
Eyal Vardi
 
Angular 2.0 Routing and Navigation
Eyal Vardi
 
Async & Parallel in JavaScript
Eyal Vardi
 
Angular 2.0 Pipes
Eyal Vardi
 
Angular 2.0 forms
Eyal Vardi
 
Ad

Recently uploaded (20)

PDF
July Patch Tuesday
Ivanti
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
July Patch Tuesday
Ivanti
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 

Controllers & actions

Editor's Notes

  • #16: http://davidhayden.com/blog/dave/archive/2011/02/09/SessionLessControllersMvc3.aspx
  • #17: http://davidhayden.com/blog/dave/archive/2011/01/25/PartialPageOutputCachingASPNETMVC3.aspx