Presentation is loading. Please wait.

Presentation is loading. Please wait.

03 | Developing MVC 4 Controllers Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek.

Similar presentations


Presentation on theme: "03 | Developing MVC 4 Controllers Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek."— Presentation transcript:

1 03 | Developing MVC 4 Controllers Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek

2 Writing Controllers Module Overview

3 Lesson 1: Writing Controllers and Actions Responding to User Requests Writing Controller Actions Using Parameters Passing Information to Views Demonstration Filters

4 Responding to User Requests When an MVC web application receives a user request, the following events occur: MVCHandler creates a controller factory. Controller factory creates a Controller object and MVCHandler calls the Execute method. ControllerActionInvoker examines RequestContext and determines the action to call. ControllerActionInvoker determines the values to be passed to the action as parameters. ControllerActionInvoker runs the action.

5 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

6 Using Parameters public ActionResult GetSessionByTitle(string title){ var query = from s in context.Sessions where s.Title == title select s Photo session = query.FirstOrDefault(); return View("Details", session); } http://www.adventureworks.com/session/getsessionbytitle?title=MVC101 DefaultModelBinder

7 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

8 DEMO How to Create a Controller

9 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.

10 ©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "03 | Developing MVC 4 Controllers Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek."

Similar presentations


Ads by Google