SlideShare a Scribd company logo
Working with
Controllers and Actions
in MVC




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Objectives
• Learn how controllers manage MVC applications
• Understand action methods and how they can
  receive input
• Explore how you can return a result from an
  action method




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Agenda
•   Introduction to Controllers
•   Using a Controller to Manage the Application
•   Controller Actions
•   Returning Action Results




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Introduction to Controllers




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
    Responds to user input




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
    Responds to user input
    Manages overall flow of application




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
    Responds to user input
    Manages overall flow of application
    Interacts with model to change state and data




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
      Responds to user input
      Manages overall flow of application
      Interacts with model to change state and data
      Selects a result to respond to user




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
      Responds to user input
      Manages overall flow of application
      Interacts with model to change state and data
      Selects a result to respond to user
• Does not itself contain UI, data, or business logic
  code




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
      Responds to user input
      Manages overall flow of application
      Interacts with model to change state and data
      Selects a result to respond to user
• Does not itself contain UI, data, or business logic
  code
• Ultimately responsible for servicing requests



                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
      Responds to user input
      Manages overall flow of application
      Interacts with model to change state and data
      Selects a result to respond to user
• Does not itself contain UI, data, or business logic
  code
• Ultimately responsible for servicing requests
    The application’s logic


                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Introduction to Controllers
• Controller is the traffic cop to keep things smooth
      Responds to user input
      Manages overall flow of application
      Interacts with model to change state and data
      Selects a result to respond to user
• Does not itself contain UI, data, or business logic
  code
• Ultimately responsible for servicing requests
    The application’s logic
• Lots of attention in MVC 3

                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Using a Controller to Manage the
Application




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Using a Controller to Manage the
Application
  • Powerful but easy to use




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Using a Controller to Manage the
Application
  • Powerful but easy to use
  • Implemented as .NET class with properties and
    methods




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Using a Controller to Manage the
Application
  • Powerful but easy to use
  • Implemented as .NET class with properties and
    methods
  • System.Web.Mvc namespace




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Using a Controller to Manage the
Application
  • Powerful but easy to use
  • Implemented as .NET class with properties and
    methods
  • System.Web.Mvc namespace
     Your controllers are likely to need very little code




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using a Controller to Manage the
Application
  • Powerful but easy to use
  • Implemented as .NET class with properties and
    methods
  • System.Web.Mvc namespace
     Your controllers are likely to need very little code
     Routine infrastructure encapsulated in base classes




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface
   Have a name ending in “Controller”




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface
   Have a name ending in “Controller”
   Marked public, not abstract, no generic parameters




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface
   Have a name ending in “Controller”
   Marked public, not abstract, no generic parameters
• Otherwise, not recognized as controller




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface
   Have a name ending in “Controller”
   Marked public, not abstract, no generic parameters
• Otherwise, not recognized as controller
   Methods never called as action methods




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface
   Have a name ending in “Controller”
   Marked public, not abstract, no generic parameters
• Otherwise, not recognized as controller
   Methods never called as action methods
• Normally inherit from Controller class




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface
   Have a name ending in “Controller”
   Marked public, not abstract, no generic parameters
• Otherwise, not recognized as controller
   Methods never called as action methods
• Normally inherit from Controller class
   Which inherits from ControllerBase



             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controllers in System.Web.Mvc
• Controller must:
   Implement IController interface
   Have a name ending in “Controller”
   Marked public, not abstract, no generic parameters
• Otherwise, not recognized as controller
   Methods never called as action methods
• Normally inherit from Controller class
   Which inherits from ControllerBase
   Which implements IController


             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The IController Interface




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
The IController Interface
• IController requirement isn’t onerous




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The IController Interface
• IController requirement isn’t onerous
• Single purpose: find a controller and call Execute
  method




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The IController Interface
• IController requirement isn’t onerous
• Single purpose: find a controller and call Execute
  method
• Simple interface definition:




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The IController Interface
• IController requirement isn’t onerous
• Single purpose: find a controller and call Execute
  method
• Simple interface definition:
      public interface IController {
         void Execute(RequestContext requestContext);
      }




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
The IController Interface
• IController requirement isn’t onerous
• Single purpose: find a controller and call Execute
  method
• Simple interface definition:
      public interface IController {
         void Execute(RequestContext requestContext);
      }




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
The IController Interface
• IController requirement isn’t onerous
• Single purpose: find a controller and call Execute
  method
• Simple interface definition:
      public interface IController {
         void Execute(RequestContext requestContext);
      }




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
The IController Interface
• IController requirement isn’t onerous
• Single purpose: find a controller and call Execute
  method
• Simple interface definition:
      public interface IController {
         void Execute(RequestContext requestContext);
      }

• When request arrives
   Routing identifies controller, calls Execute
   Passes in object with context information

               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
The ControllerBase Abstract Base
Class




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
The ControllerBase Abstract Base
Class
  • Implements IController




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The ControllerBase Abstract Base
Class
  • Implements IController
     Adds controller features, such as TempData and
      ViewData




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The ControllerBase Abstract Base
Class
  • Implements IController
     Adds controller features, such as TempData and
      ViewData
     Execute method creates ControllerContext object




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The ControllerBase Abstract Base
Class
  • Implements IController
      Adds controller features, such as TempData and
       ViewData
      Execute method creates ControllerContext object
  • Still pretty lightweight




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The ControllerBase Abstract Base
Class
  • Implements IController
      Adds controller features, such as TempData and
       ViewData
      Execute method creates ControllerContext object
  • Still pretty lightweight
      Relatively little added functionality




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
The ControllerBase Abstract Base
Class
  • Implements IController
      Adds controller features, such as TempData and
       ViewData
      Execute method creates ControllerContext object
  • Still pretty lightweight
      Relatively little added functionality
      Could build Web site with either IController or
       ControllerBase



              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Controller Class




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure
• Inherits from ControllerBase




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure
• Inherits from ControllerBase
   So indirectly implements IController




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure
• Inherits from ControllerBase
   So indirectly implements IController
• Added features include




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure
• Inherits from ControllerBase
   So indirectly implements IController
• Added features include
   Action methods




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure
• Inherits from ControllerBase
   So indirectly implements IController
• Added features include
   Action methods
   Action results




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure
• Inherits from ControllerBase
   So indirectly implements IController
• Added features include
   Action methods
   Action results
   Filters




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Controller Class
• Rich implementation of controller infrastructure
• Inherits from ControllerBase
   So indirectly implements IController
• Added features include
   Action methods
   Action results
   Filters
• Normally should implement your controllers
  using Controller

             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The MVC Request Processing




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
The MVC Request Processing
• Response process to every user request




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
The MVC Request Processing
• Response process to every user request
   IIS, ASP.NET, MVC collaboration




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The MVC Request Processing
• Response process to every user request
    IIS, ASP.NET, MVC collaboration

Request




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The MVC Request Processing
• Response process to every user request
    IIS, ASP.NET, MVC collaboration

Request
          HTTP




                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
The MVC Request Processing
• Response process to every user request
    IIS, ASP.NET, MVC collaboration

Request
          HTTP

          Routing Engine            Controller Factory                     Controller




              View                    Action Method                     Action Invoker




                  Learn More @ http://www.learnnowonline.com
                      Copyright © by Application Developers Training Company
The MVC Request Processing
• Response process to every user request
    IIS, ASP.NET, MVC collaboration

Request
          HTTP

          Routing Engine            Controller Factory                     Controller




              View                    Action Method                     Action Invoker



                                                                                HTTP

                  Learn More @ http://www.learnnowonline.com
                      Copyright © by Application Developers Training Company
The MVC Request Processing
• Response process to every user request
    IIS, ASP.NET, MVC collaboration

Request
          HTTP

          Routing Engine            Controller Factory                     Controller




              View                    Action Method                     Action Invoker


                                                                                         Response
                                                                                HTTP

                  Learn More @ http://www.learnnowonline.com
                      Copyright © by Application Developers Training Company
Action Method Selection




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears
   Multiple overloaded methods with same name




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears
   Multiple overloaded methods with same name
   Decorated with attributes that control use




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears
   Multiple overloaded methods with same name
   Decorated with attributes that control use
   Action name may be different from method name




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears
   Multiple overloaded methods with same name
   Decorated with attributes that control use
   Action name may be different from method name
• Starts by getting action portion of route
  {controller}/{action}/{id}




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears
   Multiple overloaded methods with same name
   Decorated with attributes that control use
   Action name may be different from method name
• Starts by getting action portion of route
  {controller}/{action}/{id}
   Default action is “Index”



             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears
   Multiple overloaded methods with same name
   Decorated with attributes that control use
   Action name may be different from method name
• Starts by getting action portion of route
  {controller}/{action}/{id}
   Default action is “Index”
• Then map action name to controller method

             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Action Method Selection
• Job of the action invoker
• More complicated than initially appears
   Multiple overloaded methods with same name
   Decorated with attributes that control use
   Action name may be different from method name
• Starts by getting action portion of route
  {controller}/{action}/{id}
   Default action is “Index”
• Then map action name to controller method
   Simplest: one method with that action name
             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Action Method Qualifications




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared
   Cannot be defined on System.Object or Controller




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared
   Cannot be defined on System.Object or Controller
     o   i.e., ToString() could not be an action method




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared
   Cannot be defined on System.Object or Controller
     o   i.e., ToString() could not be an action method
   Cannot be a special method




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared
   Cannot be defined on System.Object or Controller
     o   i.e., ToString() could not be an action method
   Cannot be a special method
   Cannot have a NonAction attribute




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared
   Cannot be defined on System.Object or Controller
     o   i.e., ToString() could not be an action method
   Cannot be a special method
   Cannot have a NonAction attribute
• Action invoker uses reflection to find candidates



                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared
   Cannot be defined on System.Object or Controller
     o   i.e., ToString() could not be an action method
   Cannot be a special method
   Cannot have a NonAction attribute
• Action invoker uses reflection to find candidates
   All methods with same action name


                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Method Qualifications
• Method must meet requirements to be action
  method:
   Public and not static or Shared
   Cannot be defined on System.Object or Controller
     o   i.e., ToString() could not be an action method
   Cannot be a special method
   Cannot have a NonAction attribute
• Action invoker uses reflection to find candidates
   All methods with same action name
   But method attributes can make this murky with
    multiple candidates
                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
ActionName Attribute




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
ActionName Attribute
• Normal convention is for controller method name
  to be same as action name




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ActionName Attribute
• Normal convention is for controller method name
  to be same as action name
• Can give it a different action name




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ActionName Attribute
• Normal convention is for controller method name
  to be same as action name
• Can give it a different action name
   Action name that is not a legal language name




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
ActionName Attribute
• Normal convention is for controller method name
  to be same as action name
• Can give it a different action name
   Action name that is not a legal language name
   Use an MVC component name as action name




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
ActionName Attribute
• Normal convention is for controller method name
  to be same as action name
• Can give it a different action name
   Action name that is not a legal language name
   Use an MVC component name as action name
   Different naming standards




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
ActionMethodSelector Attribute




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request
    Single method: IsValidForRequest




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request
    Single method: IsValidForRequest
      o   Executes on each candidate action method




               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request
    Single method: IsValidForRequest
      o   Executes on each candidate action method
      o   Return false, removed from list




               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request
    Single method: IsValidForRequest
      o   Executes on each candidate action method
      o   Return false, removed from list

• Implementations



               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request
    Single method: IsValidForRequest
      o   Executes on each candidate action method
      o   Return false, removed from list

• Implementations
    NonAction attribute



               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request
    Single method: IsValidForRequest
      o   Executes on each candidate action method
      o   Return false, removed from list

• Implementations
    NonAction attribute
    AcceptVerbs attribute

               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
ActionMethodSelector Attribute
• Now action invoker has list of all matching action
  names
• Next examines ActionMethodSelector attributes
    Control what conditions a method should be used for a
     request
    Single method: IsValidForRequest
      o   Executes on each candidate action method
      o   Return false, removed from list

• Implementations
    NonAction attribute
    AcceptVerbs attribute
    HttpDelete, HttpGet, HttpPost, HttpPut
               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Controller Actions




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job
   Receive and process input




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job
   Receive and process input
   Perform processing




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job
   Receive and process input
   Perform processing
   Generate output




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job
   Receive and process input
   Perform processing
   Generate output
     o   Select a view




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job
   Receive and process input
   Perform processing
   Generate output
     o   Select a view
     o   Write output directly to page




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job
   Receive and process input
   Perform processing
   Generate output
     o   Select a view
     o   Write output directly to page
     o   Raw data to browser




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Controller Actions
• Once invoked, action method does its job
   Receive and process input
   Perform processing
   Generate output
     o   Select a view
     o   Write output directly to page
     o   Raw data to browser
     o   Do nothing




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Method Input




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
   Environment




             Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
   Environment
   Operating system




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
   Environment
   Operating system
   Nature of user request




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
     Environment
     Operating system
     Nature of user request
     Direct parameters




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
     Environment
     Operating system
     Nature of user request
     Direct parameters
     Many others




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
     Environment
     Operating system
     Nature of user request
     Direct parameters
     Many others
• Three broad sources for action method




                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
     Environment
     Operating system
     Nature of user request
     Direct parameters
     Many others
• Three broad sources for action method
   Context objects



                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
     Environment
     Operating system
     Nature of user request
     Direct parameters
     Many others
• Three broad sources for action method
   Context objects
   Method parameters


                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Action Method Input
• Most methods need data to do work
• Can come from a variety of sources
     Environment
     Operating system
     Nature of user request
     Direct parameters
     Many others
• Three broad sources for action method
   Context objects
   Method parameters
   Model bindings
                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Context Object Input




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Context Object Input
• Web requests come loaded with information




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company
Context Object Input
• Web requests come loaded with information
   HTTP headers




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Context Object Input
• Web requests come loaded with information
   HTTP headers
   User information




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Context Object Input
• Web requests come loaded with information
   HTTP headers
   User information
   Browser and capability information




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Context Object Input
• Web requests come loaded with information
     HTTP headers
     User information
     Browser and capability information
     User’s IP address




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Context Object Input
• Web requests come loaded with information
     HTTP headers
     User information
     Browser and capability information
     User’s IP address
     Authentication information




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Context Object Input
• Web requests come loaded with information
     HTTP headers
     User information
     Browser and capability information
     User’s IP address
     Authentication information
• Information available through context objects




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Parameter Input




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values
• Action invoker examines context objects for
  parameter values and uses model binder




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values
• Action invoker examines context objects for
  parameter values and uses model binder
  1. Request.Form




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values
• Action invoker examines context objects for
  parameter values and uses model binder
  1. Request.Form
  2. RouteData.Values




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values
• Action invoker examines context objects for
  parameter values and uses model binder
  1. Request.Form
  2. RouteData.Values
  3. Request.QueryString




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values
• Action invoker examines context objects for
  parameter values and uses model binder
  1. Request.Form
  2. RouteData.Values
  3. Request.QueryString
• Matches solely by name in order


            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values
• Action invoker examines context objects for
  parameter values and uses model binder
  1. Request.Form
  2. RouteData.Values
  3. Request.QueryString
• Matches solely by name in order
   Once it finds a match it stops searching


             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Parameter Input
• Specialized input customized for method
• MVC takes care of populating values
• Action invoker examines context objects for
  parameter values and uses model binder
  1. Request.Form
  2. RouteData.Values
  3. Request.QueryString
• Matches solely by name in order
   Once it finds a match it stops searching
   Have to coordinate names to avoid duplicate names
            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Model Bindings




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Model Bindings
• Model encapsulates data and business rules




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Model Bindings
• Model encapsulates data and business rules
   Use directly in controllers and views




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Model Bindings
• Model encapsulates data and business rules
   Use directly in controllers and views
   Built-in features to make easy




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Model Bindings
• Model encapsulates data and business rules
   Use directly in controllers and views
   Built-in features to make easy
   Automatic data scaffolding




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Session State




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute
   Control how and whether controller uses session state




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute
   Control how and whether controller uses session state
   MVC uses session state by default, if available




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute
   Control how and whether controller uses session state
   MVC uses session state by default, if available
• SessionStateBehavior enumeration




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute
   Control how and whether controller uses session state
   MVC uses session state by default, if available
• SessionStateBehavior enumeration
   Default




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute
   Control how and whether controller uses session state
   MVC uses session state by default, if available
• SessionStateBehavior enumeration
   Default
   Disabled




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute
   Control how and whether controller uses session state
   MVC uses session state by default, if available
• SessionStateBehavior enumeration
   Default
   Disabled
   ReadOnly




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Controller Session State
• SessionState attribute
   Control how and whether controller uses session state
   MVC uses session state by default, if available
• SessionStateBehavior enumeration
     Default
     Disabled
     ReadOnly
     Required




             Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Returning Action Results




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request
   Generates response




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request
   Generates response
• Many possible responses




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request
   Generates response
• Many possible responses
• Three broad types of results




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request
   Generates response
• Many possible responses
• Three broad types of results
   HTML




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request
   Generates response
• Many possible responses
• Three broad types of results
   HTML
   Redirect somewhere else



            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request
   Generates response
• Many possible responses
• Three broad types of results
   HTML
   Redirect somewhere else
   Page data


            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Action Results
• Action method follows a normal process
   Receives request and data
   Process the request
   Generates response
• Many possible responses
• Three broad types of results
   HTML
   Redirect somewhere else
   Page data
• All options based on ActionResult
            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Result




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Action Result
• ActionResult base class




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Action Result
• ActionResult base class
     public abstract class ActionResult {
        public abstract void ExecuteResult(ControllerContext context);
     }




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Result
• ActionResult base class
     public abstract class ActionResult {
        public abstract void ExecuteResult(ControllerContext context);
     }




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Action Result
• ActionResult base class
     public abstract class ActionResult {
        public abstract void ExecuteResult(ControllerContext context);
     }

• ExecuteResult receives context information
• Takes care of low-level work of generating a
  response




                Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
ActionResult Types




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
ActionResult Types
Type                          Helper Method                   Description
ContentResult                 Content()                       Raw text data
EmptyResult                   --                              Returns nothing
FileResult                    File()                          Base class to send file
HttpNotFoundResult            HttpNotFound()                  Returns HTTP 404
HttpStatusCodeResult          --                              Returns any HTTP status
HttpUnauthorizedResult        --                              Returns HTTP 401
JavaScriptResult              JavaScript()                    Returns and executes script
JsonResult                    Json()                          JavaScript Object Notation
PartialViewResult             PartialView()                   HTML snippet
RedirectResult                Redirect()                      Redirect to URL
RedirectToRouteResult         RedirectToRoute()               Redirect to MVC route or action
ViewResult                    RedirectToAction()
                              View()                          Full HTML page
                    Learn More @ http://www.learnnowonline.com
                        Copyright © by Application Developers Training Company
Passing Data to the View




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
   Different faces of the same feature




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
   Different faces of the same feature
   A bit like ASP.NET Session object




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
   Different faces of the same feature
   A bit like ASP.NET Session object
   Exposes ViewDataDictionary




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
     Different faces of the same feature
     A bit like ASP.NET Session object
     Exposes ViewDataDictionary
     But ViewData/ViewBag disappear after view rendered




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
     Different faces of the same feature
     A bit like ASP.NET Session object
     Exposes ViewDataDictionary
     But ViewData/ViewBag disappear after view rendered
     Session lasts for the user session




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
     Different faces of the same feature
     A bit like ASP.NET Session object
     Exposes ViewDataDictionary
     But ViewData/ViewBag disappear after view rendered
     Session lasts for the user session
• ViewData is loosely typed




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
     Different faces of the same feature
     A bit like ASP.NET Session object
     Exposes ViewDataDictionary
     But ViewData/ViewBag disappear after view rendered
     Session lasts for the user session
• ViewData is loosely typed
• ViewBag is strongly typed



              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Passing Data to the View
• Use either ViewData or ViewBag
     Different faces of the same feature
     A bit like ASP.NET Session object
     Exposes ViewDataDictionary
     But ViewData/ViewBag disappear after view rendered
     Session lasts for the user session
• ViewData is loosely typed
• ViewBag is strongly typed
   Uses dynamic language feature of C# and VB


              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
ViewData and ViewBag




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
ViewData and ViewBag
• Not limited to passing strings




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
ViewData and ViewBag
• Not limited to passing strings
• Keep in mind that they are same feature, but
  different




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ViewData and ViewBag
• Not limited to passing strings
• Keep in mind that they are same feature, but
  different
   Can mix and match in one action method/view




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ViewData and ViewBag
• Not limited to passing strings
• Keep in mind that they are same feature, but
  different
   Can mix and match in one action method/view
   Get the same result either way




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ViewData and ViewBag
• Not limited to passing strings
• Keep in mind that they are same feature, but
  different
   Can mix and match in one action method/view
   Get the same result either way
   Generally should use ViewBag




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
ViewData and ViewBag
• Not limited to passing strings
• Keep in mind that they are same feature, but
  different
    Can mix and match in one action method/view
    Get the same result either way
    Generally should use ViewBag
• But in VB, using in controller requires setting
  Option Strict Off



             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Returning Text Data: ContentResult




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Returning Text Data: ContentResult
 • HTML is dominant data format




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Returning Text Data: ContentResult
 • HTML is dominant data format
    But browsers can deal with variety of formats




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Returning Text Data: ContentResult
 • HTML is dominant data format
    But browsers can deal with variety of formats
 • Use a ContentResult for other formats




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Returning Text Data: ContentResult
 • HTML is dominant data format
    But browsers can deal with variety of formats
 • Use a ContentResult for other formats
    Content helper method




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Returning Text Data: ContentResult
 • HTML is dominant data format
    But browsers can deal with variety of formats
 • Use a ContentResult for other formats
    Content helper method
    Specify content as string




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Returning Text Data: ContentResult
 • HTML is dominant data format
    But browsers can deal with variety of formats
 • Use a ContentResult for other formats
    Content helper method
    Specify content as string
    Optionally specify type, such as text/xml




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Implicit Action Results




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Implicit Action Results
• Return text and don’t need to specify type




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Implicit Action Results
• Return text and don’t need to specify type
• If type is not string, calls ToString method with
  InvariantCulture setting




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Implicit Action Results
• Return text and don’t need to specify type
• If type is not string, calls ToString method with
  InvariantCulture setting
    Wraps in ContentResult object




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Missing Resource:




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Missing Resource:
• Useful to indicate that requested resource is not
  available




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Missing Resource:
• Useful to indicate that requested resource is not
  available
   User hacks a URL




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Missing Resource:
• Useful to indicate that requested resource is not
  available
   User hacks a URL
• Returns HTTP 404 status code




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Missing Resource:
• Useful to indicate that requested resource is not
  available
   User hacks a URL
• Returns HTTP 404 status code
   Can mask an exception that an attacker could
    otherwise use




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Return any HTTP Status Code:




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Return any HTTP Status Code:
• More flexible than HttpNotFoundResult




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Return any HTTP Status Code:
• More flexible than HttpNotFoundResult
• Can specify any HTTP status code




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Return any HTTP Status Code:
• More flexible than HttpNotFoundResult
• Can specify any HTTP status code
   Optional description




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Redirection Methods




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods
   Permanent property set to true




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods
   Permanent property set to true
• Types




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods
   Permanent property set to true
• Types
   RedirectPermanent method




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods
   Permanent property set to true
• Types
   RedirectPermanent method
   RedirectToRoutePermanent method




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods
   Permanent property set to true
• Types
   RedirectPermanent method
   RedirectToRoutePermanent method
   RedirectToActionPermanent method




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods
   Permanent property set to true
• Types
   RedirectPermanent method
   RedirectToRoutePermanent method
   RedirectToActionPermanent method
• Make easy to manage permanent redirections


             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Redirection Methods
• Methods on the Controller class
• Return instances of redirection action methods
   Permanent property set to true
• Types
   RedirectPermanent method
   RedirectToRoutePermanent method
   RedirectToActionPermanent method
• Make easy to manage permanent redirections
   HTTP 301 status code


             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Learn More!




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
Learn More!
• This is an excerpt from a larger course. Visit
  www.learnnowonline.com for the full details!




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company

More Related Content

What's hot (18)

PPTX
Eliminate Risks in SOA Implementation & Support
SuneraTech
 
PPTX
Accessibility testing technology, human touch and value
Srinivasu Chakravarthula
 
PPTX
Appmotives - Software Testing As Service
Kalyan Paluri
 
PPTX
The Powerful and Comprehensive API for Mobile App Development and Testing
Bitbar
 
PDF
Atagg 2015 Test automation and effective continuous integration
Agile Testing Alliance
 
PDF
Helping Organizations Realize the Value of DevOps with Continuous Software De...
IBM UrbanCode Products
 
PDF
Uncovering breaking changes behind UI on mobile applications
Kazuaki Matsuo
 
PDF
How Financial Engines Drives Business Outcomes Using AppDynamics Analytics - ...
AppDynamics
 
PPT
DevBeat 2013 IBM Master Class presentation
Leigh Williamson
 
PPT
Test Automation Framework Online Training by QuontraSolutions
Quontra Solutions
 
DOCX
15.3 student guide web application tool time overviewtodays c
UMAR48665
 
PDF
Test Automation Frameworks Using Selenium | Edureka
Edureka!
 
PDF
Performance Testing Insights
Deepu S Nath
 
PDF
Top 10 Automation Testing Tools in 2020
Alaina Carter
 
PPTX
How To Sell Into Insurance with Perfecto
Lizzy Guido (she/her)
 
PDF
API Integration For Building Software Applications Powerpoint Presentation Sl...
SlideTeam
 
PDF
Building Effective and Rapid Applications with IBM MobileFirst Platform
Andrew Ferrier
 
DOCX
ansari
Wasim Ansari
 
Eliminate Risks in SOA Implementation & Support
SuneraTech
 
Accessibility testing technology, human touch and value
Srinivasu Chakravarthula
 
Appmotives - Software Testing As Service
Kalyan Paluri
 
The Powerful and Comprehensive API for Mobile App Development and Testing
Bitbar
 
Atagg 2015 Test automation and effective continuous integration
Agile Testing Alliance
 
Helping Organizations Realize the Value of DevOps with Continuous Software De...
IBM UrbanCode Products
 
Uncovering breaking changes behind UI on mobile applications
Kazuaki Matsuo
 
How Financial Engines Drives Business Outcomes Using AppDynamics Analytics - ...
AppDynamics
 
DevBeat 2013 IBM Master Class presentation
Leigh Williamson
 
Test Automation Framework Online Training by QuontraSolutions
Quontra Solutions
 
15.3 student guide web application tool time overviewtodays c
UMAR48665
 
Test Automation Frameworks Using Selenium | Edureka
Edureka!
 
Performance Testing Insights
Deepu S Nath
 
Top 10 Automation Testing Tools in 2020
Alaina Carter
 
How To Sell Into Insurance with Perfecto
Lizzy Guido (she/her)
 
API Integration For Building Software Applications Powerpoint Presentation Sl...
SlideTeam
 
Building Effective and Rapid Applications with IBM MobileFirst Platform
Andrew Ferrier
 
ansari
Wasim Ansari
 

Viewers also liked (13)

PPTX
Electronic controllers presentation
Akshay Dhole
 
PDF
Time Response
Syed Saeed
 
PDF
Class 23 electronic controllers
Manipal Institute of Technology
 
PPTX
Pid controllers
milind1076
 
PPTX
PID Controller
saishah72
 
PPTX
Block Diagram For Control Systems.
Suleyman Demirel University
 
PPTX
p i d controller
hammama syed
 
PDF
Proportional-Derivative-Integral (PID) Control
guest9006ab
 
PPT
Controller ppt
gourav0077
 
PPTX
Traffic light controller
Rkrishna Mishra
 
PPT
Basic types of facts controllers
Ayyarao T S L V
 
PPT
Active Directory Training
Nishad Sukumaran
 
PPTX
Slideshare ppt
Mandy Suzanne
 
Electronic controllers presentation
Akshay Dhole
 
Time Response
Syed Saeed
 
Class 23 electronic controllers
Manipal Institute of Technology
 
Pid controllers
milind1076
 
PID Controller
saishah72
 
Block Diagram For Control Systems.
Suleyman Demirel University
 
p i d controller
hammama syed
 
Proportional-Derivative-Integral (PID) Control
guest9006ab
 
Controller ppt
gourav0077
 
Traffic light controller
Rkrishna Mishra
 
Basic types of facts controllers
Ayyarao T S L V
 
Active Directory Training
Nishad Sukumaran
 
Slideshare ppt
Mandy Suzanne
 
Ad

Similar to Working with Controllers and Actions in MVC (20)

KEY
Introduction to ASP.NET MVC
LearnNowOnline
 
PPTX
Chapter4.pptx
narendrakumar406336
 
PDF
ASP.NET MVC 2.0
Buu Nguyen
 
PPTX
Week 2- Controllers in ASP .NET Core MVC Web Application.pptx
Naser Chowdhury
 
PDF
Zen and the art of the controller
Michael Kelly
 
PPTX
2011 NetUG HH: ASP.NET MVC & HTML 5
Daniel Fisher
 
PDF
ASP.NET MVC - Whats The Big Deal
Venketash (Pat) Ramadass
 
PDF
L13 Presentation Layer Design
Ólafur Andri Ragnarsson
 
PPTX
Deep Dive: MVC Controller Architecture
Chris Eargle
 
PPTX
ASP.MVC Training
Mahesh Sikakolli
 
PDF
Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC
Mohamed Meligy
 
PDF
Aspnet Mvc 2 In Action 1st Edition Jeffery Palermo Ben Scheirman
wljuodqyy180
 
PPTX
Model view controller (mvc)
M Ahsan Khan
 
PPT
CTTDNUG ASP.NET MVC
Barry Gervin
 
PPTX
Model View Controller(MVC)
Himanshu Chawla
 
PPTX
Introduction to ASP.Net MVC
Sagar Kamate
 
PDF
Asp.Net MVC Framework Design Pattern
maddinapudi
 
PDF
Programming ASP NET MVC 4 Developing Real World Web Applications with ASP NET...
aalbxlrt993
 
PPTX
Intro ASP MVC
KrishnaPPatel
 
PPTX
Model View Controller ext4
Pankaj Avhad
 
Introduction to ASP.NET MVC
LearnNowOnline
 
Chapter4.pptx
narendrakumar406336
 
ASP.NET MVC 2.0
Buu Nguyen
 
Week 2- Controllers in ASP .NET Core MVC Web Application.pptx
Naser Chowdhury
 
Zen and the art of the controller
Michael Kelly
 
2011 NetUG HH: ASP.NET MVC & HTML 5
Daniel Fisher
 
ASP.NET MVC - Whats The Big Deal
Venketash (Pat) Ramadass
 
L13 Presentation Layer Design
Ólafur Andri Ragnarsson
 
Deep Dive: MVC Controller Architecture
Chris Eargle
 
ASP.MVC Training
Mahesh Sikakolli
 
Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC
Mohamed Meligy
 
Aspnet Mvc 2 In Action 1st Edition Jeffery Palermo Ben Scheirman
wljuodqyy180
 
Model view controller (mvc)
M Ahsan Khan
 
CTTDNUG ASP.NET MVC
Barry Gervin
 
Model View Controller(MVC)
Himanshu Chawla
 
Introduction to ASP.Net MVC
Sagar Kamate
 
Asp.Net MVC Framework Design Pattern
maddinapudi
 
Programming ASP NET MVC 4 Developing Real World Web Applications with ASP NET...
aalbxlrt993
 
Intro ASP MVC
KrishnaPPatel
 
Model View Controller ext4
Pankaj Avhad
 
Ad

More from LearnNowOnline (20)

PPT
Windows 8: Shapes and Geometries
LearnNowOnline
 
PPT
SQL: Permissions and Data Protection
LearnNowOnline
 
PPT
New in the Visual Studio 2012 IDE
LearnNowOnline
 
KEY
Attributes, reflection, and dynamic programming
LearnNowOnline
 
KEY
Asynchronous Programming
LearnNowOnline
 
KEY
WPF: Working with Data
LearnNowOnline
 
KEY
WPF Binding
LearnNowOnline
 
KEY
A tour of SQL Server
LearnNowOnline
 
KEY
Introducing LINQ
LearnNowOnline
 
KEY
Generics
LearnNowOnline
 
KEY
Object oriented techniques
LearnNowOnline
 
KEY
Object-Oriented JavaScript
LearnNowOnline
 
KEY
SharePoint Document Management
LearnNowOnline
 
KEY
SharePoint: Introduction to InfoPath
LearnNowOnline
 
KEY
Managing site collections
LearnNowOnline
 
KEY
Web API HTTP Pipeline
LearnNowOnline
 
KEY
Web API Basics
LearnNowOnline
 
KEY
SQL Server: Security
LearnNowOnline
 
KEY
Sql 2012 development and programming
LearnNowOnline
 
KEY
What's new in Silverlight 5
LearnNowOnline
 
Windows 8: Shapes and Geometries
LearnNowOnline
 
SQL: Permissions and Data Protection
LearnNowOnline
 
New in the Visual Studio 2012 IDE
LearnNowOnline
 
Attributes, reflection, and dynamic programming
LearnNowOnline
 
Asynchronous Programming
LearnNowOnline
 
WPF: Working with Data
LearnNowOnline
 
WPF Binding
LearnNowOnline
 
A tour of SQL Server
LearnNowOnline
 
Introducing LINQ
LearnNowOnline
 
Generics
LearnNowOnline
 
Object oriented techniques
LearnNowOnline
 
Object-Oriented JavaScript
LearnNowOnline
 
SharePoint Document Management
LearnNowOnline
 
SharePoint: Introduction to InfoPath
LearnNowOnline
 
Managing site collections
LearnNowOnline
 
Web API HTTP Pipeline
LearnNowOnline
 
Web API Basics
LearnNowOnline
 
SQL Server: Security
LearnNowOnline
 
Sql 2012 development and programming
LearnNowOnline
 
What's new in Silverlight 5
LearnNowOnline
 

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 

Working with Controllers and Actions in MVC

  • 1. Working with Controllers and Actions in MVC Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 2. Objectives • Learn how controllers manage MVC applications • Understand action methods and how they can receive input • Explore how you can return a result from an action method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 3. Agenda • Introduction to Controllers • Using a Controller to Manage the Application • Controller Actions • Returning Action Results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 4. Introduction to Controllers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 5. Introduction to Controllers • Controller is the traffic cop to keep things smooth Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 6. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 7. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input  Manages overall flow of application Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 8. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input  Manages overall flow of application  Interacts with model to change state and data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 9. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input  Manages overall flow of application  Interacts with model to change state and data  Selects a result to respond to user Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 10. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input  Manages overall flow of application  Interacts with model to change state and data  Selects a result to respond to user • Does not itself contain UI, data, or business logic code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 11. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input  Manages overall flow of application  Interacts with model to change state and data  Selects a result to respond to user • Does not itself contain UI, data, or business logic code • Ultimately responsible for servicing requests Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 12. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input  Manages overall flow of application  Interacts with model to change state and data  Selects a result to respond to user • Does not itself contain UI, data, or business logic code • Ultimately responsible for servicing requests  The application’s logic Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 13. Introduction to Controllers • Controller is the traffic cop to keep things smooth  Responds to user input  Manages overall flow of application  Interacts with model to change state and data  Selects a result to respond to user • Does not itself contain UI, data, or business logic code • Ultimately responsible for servicing requests  The application’s logic • Lots of attention in MVC 3 Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 14. Using a Controller to Manage the Application Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 15. Using a Controller to Manage the Application • Powerful but easy to use Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 16. Using a Controller to Manage the Application • Powerful but easy to use • Implemented as .NET class with properties and methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 17. Using a Controller to Manage the Application • Powerful but easy to use • Implemented as .NET class with properties and methods • System.Web.Mvc namespace Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 18. Using a Controller to Manage the Application • Powerful but easy to use • Implemented as .NET class with properties and methods • System.Web.Mvc namespace  Your controllers are likely to need very little code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 19. Using a Controller to Manage the Application • Powerful but easy to use • Implemented as .NET class with properties and methods • System.Web.Mvc namespace  Your controllers are likely to need very little code  Routine infrastructure encapsulated in base classes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 20. Controllers in System.Web.Mvc Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 21. Controllers in System.Web.Mvc • Controller must: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 22. Controllers in System.Web.Mvc • Controller must:  Implement IController interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 23. Controllers in System.Web.Mvc • Controller must:  Implement IController interface  Have a name ending in “Controller” Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 24. Controllers in System.Web.Mvc • Controller must:  Implement IController interface  Have a name ending in “Controller”  Marked public, not abstract, no generic parameters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 25. Controllers in System.Web.Mvc • Controller must:  Implement IController interface  Have a name ending in “Controller”  Marked public, not abstract, no generic parameters • Otherwise, not recognized as controller Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 26. Controllers in System.Web.Mvc • Controller must:  Implement IController interface  Have a name ending in “Controller”  Marked public, not abstract, no generic parameters • Otherwise, not recognized as controller  Methods never called as action methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 27. Controllers in System.Web.Mvc • Controller must:  Implement IController interface  Have a name ending in “Controller”  Marked public, not abstract, no generic parameters • Otherwise, not recognized as controller  Methods never called as action methods • Normally inherit from Controller class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 28. Controllers in System.Web.Mvc • Controller must:  Implement IController interface  Have a name ending in “Controller”  Marked public, not abstract, no generic parameters • Otherwise, not recognized as controller  Methods never called as action methods • Normally inherit from Controller class  Which inherits from ControllerBase Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 29. Controllers in System.Web.Mvc • Controller must:  Implement IController interface  Have a name ending in “Controller”  Marked public, not abstract, no generic parameters • Otherwise, not recognized as controller  Methods never called as action methods • Normally inherit from Controller class  Which inherits from ControllerBase  Which implements IController Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 30. The IController Interface Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 31. The IController Interface • IController requirement isn’t onerous Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 32. The IController Interface • IController requirement isn’t onerous • Single purpose: find a controller and call Execute method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 33. The IController Interface • IController requirement isn’t onerous • Single purpose: find a controller and call Execute method • Simple interface definition: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 34. The IController Interface • IController requirement isn’t onerous • Single purpose: find a controller and call Execute method • Simple interface definition: public interface IController { void Execute(RequestContext requestContext); } Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 35. The IController Interface • IController requirement isn’t onerous • Single purpose: find a controller and call Execute method • Simple interface definition: public interface IController { void Execute(RequestContext requestContext); } Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 36. The IController Interface • IController requirement isn’t onerous • Single purpose: find a controller and call Execute method • Simple interface definition: public interface IController { void Execute(RequestContext requestContext); } Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 37. The IController Interface • IController requirement isn’t onerous • Single purpose: find a controller and call Execute method • Simple interface definition: public interface IController { void Execute(RequestContext requestContext); } • When request arrives  Routing identifies controller, calls Execute  Passes in object with context information Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 38. The ControllerBase Abstract Base Class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 39. The ControllerBase Abstract Base Class • Implements IController Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 40. The ControllerBase Abstract Base Class • Implements IController  Adds controller features, such as TempData and ViewData Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 41. The ControllerBase Abstract Base Class • Implements IController  Adds controller features, such as TempData and ViewData  Execute method creates ControllerContext object Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 42. The ControllerBase Abstract Base Class • Implements IController  Adds controller features, such as TempData and ViewData  Execute method creates ControllerContext object • Still pretty lightweight Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 43. The ControllerBase Abstract Base Class • Implements IController  Adds controller features, such as TempData and ViewData  Execute method creates ControllerContext object • Still pretty lightweight  Relatively little added functionality Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 44. The ControllerBase Abstract Base Class • Implements IController  Adds controller features, such as TempData and ViewData  Execute method creates ControllerContext object • Still pretty lightweight  Relatively little added functionality  Could build Web site with either IController or ControllerBase Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 45. The Controller Class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 46. The Controller Class • Rich implementation of controller infrastructure Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 47. The Controller Class • Rich implementation of controller infrastructure • Inherits from ControllerBase Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 48. The Controller Class • Rich implementation of controller infrastructure • Inherits from ControllerBase  So indirectly implements IController Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 49. The Controller Class • Rich implementation of controller infrastructure • Inherits from ControllerBase  So indirectly implements IController • Added features include Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 50. The Controller Class • Rich implementation of controller infrastructure • Inherits from ControllerBase  So indirectly implements IController • Added features include  Action methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 51. The Controller Class • Rich implementation of controller infrastructure • Inherits from ControllerBase  So indirectly implements IController • Added features include  Action methods  Action results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 52. The Controller Class • Rich implementation of controller infrastructure • Inherits from ControllerBase  So indirectly implements IController • Added features include  Action methods  Action results  Filters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 53. The Controller Class • Rich implementation of controller infrastructure • Inherits from ControllerBase  So indirectly implements IController • Added features include  Action methods  Action results  Filters • Normally should implement your controllers using Controller Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 54. The MVC Request Processing Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 55. The MVC Request Processing • Response process to every user request Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 56. The MVC Request Processing • Response process to every user request  IIS, ASP.NET, MVC collaboration Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 57. The MVC Request Processing • Response process to every user request  IIS, ASP.NET, MVC collaboration Request Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 58. The MVC Request Processing • Response process to every user request  IIS, ASP.NET, MVC collaboration Request HTTP Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 59. The MVC Request Processing • Response process to every user request  IIS, ASP.NET, MVC collaboration Request HTTP Routing Engine Controller Factory Controller View Action Method Action Invoker Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 60. The MVC Request Processing • Response process to every user request  IIS, ASP.NET, MVC collaboration Request HTTP Routing Engine Controller Factory Controller View Action Method Action Invoker HTTP Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 61. The MVC Request Processing • Response process to every user request  IIS, ASP.NET, MVC collaboration Request HTTP Routing Engine Controller Factory Controller View Action Method Action Invoker Response HTTP Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 62. Action Method Selection Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 63. Action Method Selection • Job of the action invoker Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 64. Action Method Selection • Job of the action invoker • More complicated than initially appears Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 65. Action Method Selection • Job of the action invoker • More complicated than initially appears  Multiple overloaded methods with same name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 66. Action Method Selection • Job of the action invoker • More complicated than initially appears  Multiple overloaded methods with same name  Decorated with attributes that control use Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 67. Action Method Selection • Job of the action invoker • More complicated than initially appears  Multiple overloaded methods with same name  Decorated with attributes that control use  Action name may be different from method name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 68. Action Method Selection • Job of the action invoker • More complicated than initially appears  Multiple overloaded methods with same name  Decorated with attributes that control use  Action name may be different from method name • Starts by getting action portion of route {controller}/{action}/{id} Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 69. Action Method Selection • Job of the action invoker • More complicated than initially appears  Multiple overloaded methods with same name  Decorated with attributes that control use  Action name may be different from method name • Starts by getting action portion of route {controller}/{action}/{id}  Default action is “Index” Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 70. Action Method Selection • Job of the action invoker • More complicated than initially appears  Multiple overloaded methods with same name  Decorated with attributes that control use  Action name may be different from method name • Starts by getting action portion of route {controller}/{action}/{id}  Default action is “Index” • Then map action name to controller method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 71. Action Method Selection • Job of the action invoker • More complicated than initially appears  Multiple overloaded methods with same name  Decorated with attributes that control use  Action name may be different from method name • Starts by getting action portion of route {controller}/{action}/{id}  Default action is “Index” • Then map action name to controller method  Simplest: one method with that action name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 72. Action Method Qualifications Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 73. Action Method Qualifications • Method must meet requirements to be action method: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 74. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 75. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared  Cannot be defined on System.Object or Controller Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 76. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared  Cannot be defined on System.Object or Controller o i.e., ToString() could not be an action method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 77. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared  Cannot be defined on System.Object or Controller o i.e., ToString() could not be an action method  Cannot be a special method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 78. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared  Cannot be defined on System.Object or Controller o i.e., ToString() could not be an action method  Cannot be a special method  Cannot have a NonAction attribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 79. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared  Cannot be defined on System.Object or Controller o i.e., ToString() could not be an action method  Cannot be a special method  Cannot have a NonAction attribute • Action invoker uses reflection to find candidates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 80. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared  Cannot be defined on System.Object or Controller o i.e., ToString() could not be an action method  Cannot be a special method  Cannot have a NonAction attribute • Action invoker uses reflection to find candidates  All methods with same action name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 81. Action Method Qualifications • Method must meet requirements to be action method:  Public and not static or Shared  Cannot be defined on System.Object or Controller o i.e., ToString() could not be an action method  Cannot be a special method  Cannot have a NonAction attribute • Action invoker uses reflection to find candidates  All methods with same action name  But method attributes can make this murky with multiple candidates Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 82. ActionName Attribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 83. ActionName Attribute • Normal convention is for controller method name to be same as action name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 84. ActionName Attribute • Normal convention is for controller method name to be same as action name • Can give it a different action name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 85. ActionName Attribute • Normal convention is for controller method name to be same as action name • Can give it a different action name  Action name that is not a legal language name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 86. ActionName Attribute • Normal convention is for controller method name to be same as action name • Can give it a different action name  Action name that is not a legal language name  Use an MVC component name as action name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 87. ActionName Attribute • Normal convention is for controller method name to be same as action name • Can give it a different action name  Action name that is not a legal language name  Use an MVC component name as action name  Different naming standards Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 88. ActionMethodSelector Attribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 89. ActionMethodSelector Attribute • Now action invoker has list of all matching action names Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 90. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 91. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 92. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request  Single method: IsValidForRequest Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 93. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request  Single method: IsValidForRequest o Executes on each candidate action method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 94. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request  Single method: IsValidForRequest o Executes on each candidate action method o Return false, removed from list Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 95. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request  Single method: IsValidForRequest o Executes on each candidate action method o Return false, removed from list • Implementations Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 96. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request  Single method: IsValidForRequest o Executes on each candidate action method o Return false, removed from list • Implementations  NonAction attribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 97. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request  Single method: IsValidForRequest o Executes on each candidate action method o Return false, removed from list • Implementations  NonAction attribute  AcceptVerbs attribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 98. ActionMethodSelector Attribute • Now action invoker has list of all matching action names • Next examines ActionMethodSelector attributes  Control what conditions a method should be used for a request  Single method: IsValidForRequest o Executes on each candidate action method o Return false, removed from list • Implementations  NonAction attribute  AcceptVerbs attribute  HttpDelete, HttpGet, HttpPost, HttpPut Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 99. Controller Actions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 100. Controller Actions • Once invoked, action method does its job Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 101. Controller Actions • Once invoked, action method does its job  Receive and process input Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 102. Controller Actions • Once invoked, action method does its job  Receive and process input  Perform processing Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 103. Controller Actions • Once invoked, action method does its job  Receive and process input  Perform processing  Generate output Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 104. Controller Actions • Once invoked, action method does its job  Receive and process input  Perform processing  Generate output o Select a view Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 105. Controller Actions • Once invoked, action method does its job  Receive and process input  Perform processing  Generate output o Select a view o Write output directly to page Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 106. Controller Actions • Once invoked, action method does its job  Receive and process input  Perform processing  Generate output o Select a view o Write output directly to page o Raw data to browser Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 107. Controller Actions • Once invoked, action method does its job  Receive and process input  Perform processing  Generate output o Select a view o Write output directly to page o Raw data to browser o Do nothing Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 108. Action Method Input Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 109. Action Method Input • Most methods need data to do work Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 110. Action Method Input • Most methods need data to do work • Can come from a variety of sources Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 111. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 112. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 113. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system  Nature of user request Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 114. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system  Nature of user request  Direct parameters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 115. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system  Nature of user request  Direct parameters  Many others Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 116. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system  Nature of user request  Direct parameters  Many others • Three broad sources for action method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 117. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system  Nature of user request  Direct parameters  Many others • Three broad sources for action method  Context objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 118. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system  Nature of user request  Direct parameters  Many others • Three broad sources for action method  Context objects  Method parameters Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 119. Action Method Input • Most methods need data to do work • Can come from a variety of sources  Environment  Operating system  Nature of user request  Direct parameters  Many others • Three broad sources for action method  Context objects  Method parameters  Model bindings Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 120. Context Object Input Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 121. Context Object Input • Web requests come loaded with information Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 122. Context Object Input • Web requests come loaded with information  HTTP headers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 123. Context Object Input • Web requests come loaded with information  HTTP headers  User information Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 124. Context Object Input • Web requests come loaded with information  HTTP headers  User information  Browser and capability information Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 125. Context Object Input • Web requests come loaded with information  HTTP headers  User information  Browser and capability information  User’s IP address Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 126. Context Object Input • Web requests come loaded with information  HTTP headers  User information  Browser and capability information  User’s IP address  Authentication information Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 127. Context Object Input • Web requests come loaded with information  HTTP headers  User information  Browser and capability information  User’s IP address  Authentication information • Information available through context objects Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 128. Parameter Input Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 129. Parameter Input • Specialized input customized for method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 130. Parameter Input • Specialized input customized for method • MVC takes care of populating values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 131. Parameter Input • Specialized input customized for method • MVC takes care of populating values • Action invoker examines context objects for parameter values and uses model binder Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 132. Parameter Input • Specialized input customized for method • MVC takes care of populating values • Action invoker examines context objects for parameter values and uses model binder 1. Request.Form Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 133. Parameter Input • Specialized input customized for method • MVC takes care of populating values • Action invoker examines context objects for parameter values and uses model binder 1. Request.Form 2. RouteData.Values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 134. Parameter Input • Specialized input customized for method • MVC takes care of populating values • Action invoker examines context objects for parameter values and uses model binder 1. Request.Form 2. RouteData.Values 3. Request.QueryString Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 135. Parameter Input • Specialized input customized for method • MVC takes care of populating values • Action invoker examines context objects for parameter values and uses model binder 1. Request.Form 2. RouteData.Values 3. Request.QueryString • Matches solely by name in order Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 136. Parameter Input • Specialized input customized for method • MVC takes care of populating values • Action invoker examines context objects for parameter values and uses model binder 1. Request.Form 2. RouteData.Values 3. Request.QueryString • Matches solely by name in order  Once it finds a match it stops searching Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 137. Parameter Input • Specialized input customized for method • MVC takes care of populating values • Action invoker examines context objects for parameter values and uses model binder 1. Request.Form 2. RouteData.Values 3. Request.QueryString • Matches solely by name in order  Once it finds a match it stops searching  Have to coordinate names to avoid duplicate names Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 138. Model Bindings Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 139. Model Bindings • Model encapsulates data and business rules Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 140. Model Bindings • Model encapsulates data and business rules  Use directly in controllers and views Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 141. Model Bindings • Model encapsulates data and business rules  Use directly in controllers and views  Built-in features to make easy Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 142. Model Bindings • Model encapsulates data and business rules  Use directly in controllers and views  Built-in features to make easy  Automatic data scaffolding Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 143. Controller Session State Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 144. Controller Session State • SessionState attribute Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 145. Controller Session State • SessionState attribute  Control how and whether controller uses session state Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 146. Controller Session State • SessionState attribute  Control how and whether controller uses session state  MVC uses session state by default, if available Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 147. Controller Session State • SessionState attribute  Control how and whether controller uses session state  MVC uses session state by default, if available • SessionStateBehavior enumeration Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 148. Controller Session State • SessionState attribute  Control how and whether controller uses session state  MVC uses session state by default, if available • SessionStateBehavior enumeration  Default Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 149. Controller Session State • SessionState attribute  Control how and whether controller uses session state  MVC uses session state by default, if available • SessionStateBehavior enumeration  Default  Disabled Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 150. Controller Session State • SessionState attribute  Control how and whether controller uses session state  MVC uses session state by default, if available • SessionStateBehavior enumeration  Default  Disabled  ReadOnly Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 151. Controller Session State • SessionState attribute  Control how and whether controller uses session state  MVC uses session state by default, if available • SessionStateBehavior enumeration  Default  Disabled  ReadOnly  Required Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 152. Returning Action Results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 153. Returning Action Results • Action method follows a normal process Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 154. Returning Action Results • Action method follows a normal process  Receives request and data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 155. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 156. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request  Generates response Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 157. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request  Generates response • Many possible responses Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 158. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request  Generates response • Many possible responses • Three broad types of results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 159. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request  Generates response • Many possible responses • Three broad types of results  HTML Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 160. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request  Generates response • Many possible responses • Three broad types of results  HTML  Redirect somewhere else Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 161. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request  Generates response • Many possible responses • Three broad types of results  HTML  Redirect somewhere else  Page data Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 162. Returning Action Results • Action method follows a normal process  Receives request and data  Process the request  Generates response • Many possible responses • Three broad types of results  HTML  Redirect somewhere else  Page data • All options based on ActionResult Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 163. Action Result Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 164. Action Result • ActionResult base class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 165. Action Result • ActionResult base class public abstract class ActionResult { public abstract void ExecuteResult(ControllerContext context); } Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 166. Action Result • ActionResult base class public abstract class ActionResult { public abstract void ExecuteResult(ControllerContext context); } Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 167. Action Result • ActionResult base class public abstract class ActionResult { public abstract void ExecuteResult(ControllerContext context); } • ExecuteResult receives context information • Takes care of low-level work of generating a response Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 168. ActionResult Types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 169. ActionResult Types Type Helper Method Description ContentResult Content() Raw text data EmptyResult -- Returns nothing FileResult File() Base class to send file HttpNotFoundResult HttpNotFound() Returns HTTP 404 HttpStatusCodeResult -- Returns any HTTP status HttpUnauthorizedResult -- Returns HTTP 401 JavaScriptResult JavaScript() Returns and executes script JsonResult Json() JavaScript Object Notation PartialViewResult PartialView() HTML snippet RedirectResult Redirect() Redirect to URL RedirectToRouteResult RedirectToRoute() Redirect to MVC route or action ViewResult RedirectToAction() View() Full HTML page Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 170. Passing Data to the View Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 171. Passing Data to the View • Use either ViewData or ViewBag Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 172. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 173. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature  A bit like ASP.NET Session object Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 174. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature  A bit like ASP.NET Session object  Exposes ViewDataDictionary Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 175. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature  A bit like ASP.NET Session object  Exposes ViewDataDictionary  But ViewData/ViewBag disappear after view rendered Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 176. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature  A bit like ASP.NET Session object  Exposes ViewDataDictionary  But ViewData/ViewBag disappear after view rendered  Session lasts for the user session Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 177. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature  A bit like ASP.NET Session object  Exposes ViewDataDictionary  But ViewData/ViewBag disappear after view rendered  Session lasts for the user session • ViewData is loosely typed Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 178. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature  A bit like ASP.NET Session object  Exposes ViewDataDictionary  But ViewData/ViewBag disappear after view rendered  Session lasts for the user session • ViewData is loosely typed • ViewBag is strongly typed Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 179. Passing Data to the View • Use either ViewData or ViewBag  Different faces of the same feature  A bit like ASP.NET Session object  Exposes ViewDataDictionary  But ViewData/ViewBag disappear after view rendered  Session lasts for the user session • ViewData is loosely typed • ViewBag is strongly typed  Uses dynamic language feature of C# and VB Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 180. ViewData and ViewBag Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 181. ViewData and ViewBag • Not limited to passing strings Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 182. ViewData and ViewBag • Not limited to passing strings • Keep in mind that they are same feature, but different Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 183. ViewData and ViewBag • Not limited to passing strings • Keep in mind that they are same feature, but different  Can mix and match in one action method/view Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 184. ViewData and ViewBag • Not limited to passing strings • Keep in mind that they are same feature, but different  Can mix and match in one action method/view  Get the same result either way Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 185. ViewData and ViewBag • Not limited to passing strings • Keep in mind that they are same feature, but different  Can mix and match in one action method/view  Get the same result either way  Generally should use ViewBag Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 186. ViewData and ViewBag • Not limited to passing strings • Keep in mind that they are same feature, but different  Can mix and match in one action method/view  Get the same result either way  Generally should use ViewBag • But in VB, using in controller requires setting Option Strict Off Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 187. Returning Text Data: ContentResult Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 188. Returning Text Data: ContentResult • HTML is dominant data format Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 189. Returning Text Data: ContentResult • HTML is dominant data format  But browsers can deal with variety of formats Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 190. Returning Text Data: ContentResult • HTML is dominant data format  But browsers can deal with variety of formats • Use a ContentResult for other formats Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 191. Returning Text Data: ContentResult • HTML is dominant data format  But browsers can deal with variety of formats • Use a ContentResult for other formats  Content helper method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 192. Returning Text Data: ContentResult • HTML is dominant data format  But browsers can deal with variety of formats • Use a ContentResult for other formats  Content helper method  Specify content as string Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 193. Returning Text Data: ContentResult • HTML is dominant data format  But browsers can deal with variety of formats • Use a ContentResult for other formats  Content helper method  Specify content as string  Optionally specify type, such as text/xml Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 194. Implicit Action Results Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 195. Implicit Action Results • Return text and don’t need to specify type Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 196. Implicit Action Results • Return text and don’t need to specify type • If type is not string, calls ToString method with InvariantCulture setting Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 197. Implicit Action Results • Return text and don’t need to specify type • If type is not string, calls ToString method with InvariantCulture setting  Wraps in ContentResult object Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 198. Missing Resource: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 199. Missing Resource: • Useful to indicate that requested resource is not available Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 200. Missing Resource: • Useful to indicate that requested resource is not available  User hacks a URL Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 201. Missing Resource: • Useful to indicate that requested resource is not available  User hacks a URL • Returns HTTP 404 status code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 202. Missing Resource: • Useful to indicate that requested resource is not available  User hacks a URL • Returns HTTP 404 status code  Can mask an exception that an attacker could otherwise use Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 203. Return any HTTP Status Code: Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 204. Return any HTTP Status Code: • More flexible than HttpNotFoundResult Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 205. Return any HTTP Status Code: • More flexible than HttpNotFoundResult • Can specify any HTTP status code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 206. Return any HTTP Status Code: • More flexible than HttpNotFoundResult • Can specify any HTTP status code  Optional description Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 207. Redirection Methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 208. Redirection Methods • Methods on the Controller class Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 209. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 210. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods  Permanent property set to true Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 211. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods  Permanent property set to true • Types Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 212. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods  Permanent property set to true • Types  RedirectPermanent method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 213. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods  Permanent property set to true • Types  RedirectPermanent method  RedirectToRoutePermanent method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 214. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods  Permanent property set to true • Types  RedirectPermanent method  RedirectToRoutePermanent method  RedirectToActionPermanent method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 215. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods  Permanent property set to true • Types  RedirectPermanent method  RedirectToRoutePermanent method  RedirectToActionPermanent method • Make easy to manage permanent redirections Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 216. Redirection Methods • Methods on the Controller class • Return instances of redirection action methods  Permanent property set to true • Types  RedirectPermanent method  RedirectToRoutePermanent method  RedirectToActionPermanent method • Make easy to manage permanent redirections  HTTP 301 status code Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 217. Learn More! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 218. Learn More! • This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details! Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

Editor's Notes