Using Action Result Types7:31 with James Churchill
Returning a string literal from a controller action method is unusual. An action method typically returns one of the MVC action result types. Let’s see how to do that.
To follow along commiting your changes to this course, you'll need to fork the aspnet-comic-book-gallery repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork> cd aspnet-comic-book-gallery git checkout tags/v2.4 -b using-action-result-types
For more information on the .NET types used in this video, see these MSDN pages:
This Stack Overflow question will show you how to disable the Diagnostic Tools in Visual Studio.
F12- Go To Definition
ALT+F12- Peek Definition
Returning a string literal from a controller action method is unusual. 0:00 An action method typically returns one of the Mvc action result types. 0:05 Let's start by replacing our string literal with a content result object. 0:10 Content result is an action result type provided by Mvc. 0:15 Just above our existing return statement, 0:19 let's return an instance of the content result class. 0:22 Since we're no longer returning a string, 0:29 we'll need to change our methods return type to content result. 0:31 We can use the ContentResult content property to set our content. 0:37 To do that, let's add a set of curly braces 0:41 to the right of the ContentResult constructor call. 0:45 In between the curly braces, type the property name Content 0:49 followed by an equal sign in a pair of quotes. 0:53 Then let's cut and paste our string literal from here 0:57 to there and remove the extra return statement. 1:03 Let's run our website by pressing F5 and 1:08 verify that everything is still working correctly. 1:10 Remember even though we replaced our string literal with a content result 1:13 object, we still expect our detail action method to return the same content. 1:17 Don't forget we need to browse to the URL ComicBooks/Detail. 1:23 Great, we [LAUGH] didn't break anything. 1:27 Go ahead and close Chrome. 1:31 Stop the website and hide the output window. 1:33 Content result is just one of the action result types that NVC provides. 1:37 Another action result type, redirect result, 1:41 is used to redirect the user to another URL. 1:44 Let's add an if statement that will redirect the user to the home 1:47 page if today is Monday. 1:51 We can use the DateTime.Today static property to get a date time for today. 1:54 On that object there's 2:01 a DayOfWeek property that we can use to check if today is Monday. 2:04 To do that we just compare the property value 2:08 to the DayOfWeek.Monday enumeration value. 2:11 In a later course we'll cover in more detail how to work with dates and 2:15 times in C#. 2:18 If you'd like to learn more now, see the teacher's note for 2:20 links to additional resources. 2:23 Okay, inside the if statement, 2:26 let's return a new instance of the redirect result class. 2:28 In the constructor, let's pass the string literal /, 2:33 which represents the root of our website. 2:36 Hm. 2:40 There's a problem with our code. 2:40 If we hover our mouse pointer over the offending code, 2:42 we can see the error message. 2:45 Cannot implicitly convert type System.Web.Mvc.RedirectResult 2:47 to System.Web.Mvc.ContentResult. 2:53 Looks like the compiler was unable to implicitly convert 2:57 the redirect result type to our methods content result type. 3:01 We can try explicitly casting the redirect result object to content result 3:05 by typing a set of parenthesis containing the type content result. 3:10 But that doesn't work either. 3:17 Cannot convert type, RedirectResult to ContentResult. 3:19 To resolve this issue, we need a type that isn't common to both of our result types. 3:24 Let's go to the definition for 3:30 RedirectResult by putting the cursor on the constructor and pressing F12. 3:31 Since the RedirectResult class is part of Mvc and 3:36 we don't have the source code in our solution, 3:41 Visual Studio will only display the basic information for the public class members. 3:43 For our purposes, that will work just fine. 3:48 We can see that the base class is ActionResult. 3:51 Let's go back to our controller and 3:55 put our cursor on the ContentResult constructor. 3:56 This time let's hold down the alt key when pressing F12. 4:00 This allows us to peek the definition in line with our code. 4:04 If we scroll up a bit in the peek window, 4:08 we can see that the base class is also ActionResult. 4:10 So redirect result and content result have a common base class. 4:14 Let's close the peek window and change our methods return type to ActionResult. 4:19 Problem solved all of the Mvc ActionResult types share the action result base 4:27 class because of this action result is almost always used as the return type for 4:32 action methods. 4:37 Regardless of the specific ActionResult type that is being returned. 4:39 Remember the Mvc Controller base class that we're inheriting from? 4:44 The Controller base class provides a number of methods 4:48 that can help simplify our code. 4:51 We can replace our ContentResult object with a call to the Content method, 4:53 which accepts a string for the content. 4:58 We can also replace our redirect result object with a call to the Redirect 5:07 method which accepts a string for the target URL. 5:12 Let's run our website and test our changes. 5:18 Don't forget to browse to ComicBooks/Detail. 5:23 Good, we're still seeing our content. 5:29 Next let's stop the website and update our if statement, so 5:32 that we can test our call to the Redirect method. 5:35 I'm recording this video on a Tuesday. 5:40 So I'll update the day of the week to Tuesday. 5:42 Restart the website by pressing F5. 5:48 When Chrome is finished loading, browse to ComicBooks/detail. 5:50 Excellent, notice here in the address bar that we've been redirected 5:57 to the root of our website. 6:01 Which is still returning a 404 Error, go ahead and stop the website. 6:03 If you're using GitHub let's commit our changes. 6:09 Enter a commit message of Updated Detail action 6:14 method to use action result types and click the Commit All button. 6:19 We've done a number of local commits so 6:28 now would be a good time to sync with remote server. 6:30 Click Sync here in this confirmation message 6:33 to navigate to the synchronization panel. 6:36 Here's a list of our local commits in the outgoing commit section. 6:39 Click the Push link to push these commits to the server. 6:43 Okay, we're making progress. 6:48 But we're still returning a string for our content. 6:50 It's worth noting that we're not limited to returning just a plain string. 6:53 We could update our content result to include HTML Markup. 6:58 But our controller shouldn't be concerned with the presentation of our content. 7:02 As we discussed in an earlier video, controllers are coordinators. 7:07 They're responsible for 7:12 coordinating the specific actions that need to be performed. 7:13 In order to return a response for that user request. 7:16 Presenting content is a job better suited for a view. 7:20 So, how do we return a view? 7:25 In the next section, we'll add our first view. 7:27 See you see then. 7:30
You need to sign up for Treehouse in order to download course files.Sign up