100 likes | 213 Views
03 | Developing MVC 4 Controllers. Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek. Module Overview. Writing Controllers. Lesson 1: Writing Controllers and Actions.
E N D
03 | Developing MVC 4 Controllers Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek
Module Overview • Writing Controllers
Lesson 1: Writing Controllers and Actions • Responding to User Requests Writing Controller Actions Using Parameters Passing Information to Views Demonstration • Filters
Responding to User Requests When an MVC web application receives a user request, the following events occur:
Writing Controller Actions Writing a Controller action includes: • Create public method • Return a class that derives from ActionResult • Add parameters to the method • Insert code to perform the operation and return the result
Using Parameters http://www.adventureworks.com/session/getsessionbytitle?title=MVC101 DefaultModelBinder publicActionResultGetSessionByTitle(stringtitle){ varquery = froms incontext.Sessions wheres.Title== title selects Photosession = query.FirstOrDefault(); return View("Details", session); }
Passing Data to the View • Model • View(data) • Strongly typed, can be more flexible • More complex • ViewBag • Dynamic object for storing basic pieces of information • Alias for ViewData • Perfect for sending messages to the view • Only available for that action • Redirects cause the ViewBag to be emptied • TempData • Just like the ViewBag, but it’s also available on the next page
What are Filters? Some requirements cut across logical boundaries are called cross-cutting concerns. Examples include: • Authorization • Logging • Caching There are four different types of filters: • Authorization filters run before any other filter and before the code in the action method • Action filters run before and after the code in the action method • Result filters run before and after a result is returned from an action method. • Exception filters run only if the action method or another filter throws an exception.