1 00:00:00,480 --> 00:00:05,550 Returning a string literal from a controller action method is unusual. 2 00:00:05,550 --> 00:00:10,480 An action method typically returns one of the Mvc action result types. 3 00:00:10,480 --> 00:00:15,320 Let's start by replacing our string literal with a content result object. 4 00:00:15,320 --> 00:00:19,970 Content result is an action result type provided by Mvc. 5 00:00:19,970 --> 00:00:22,350 Just above our existing return statement, 6 00:00:22,350 --> 00:00:25,740 let's return an instance of the content result class. 7 00:00:29,406 --> 00:00:31,612 Since we're no longer returning a string, 8 00:00:31,612 --> 00:00:34,900 we'll need to change our methods return type to content result. 9 00:00:37,463 --> 00:00:41,900 We can use the ContentResult content property to set our content. 10 00:00:41,900 --> 00:00:45,040 To do that, let's add a set of curly braces 11 00:00:45,040 --> 00:00:47,740 to the right of the ContentResult constructor call. 12 00:00:49,050 --> 00:00:53,730 In between the curly braces, type the property name Content 13 00:00:53,730 --> 00:00:57,650 followed by an equal sign in a pair of quotes. 14 00:00:57,650 --> 00:01:01,530 Then let's cut and paste our string literal from here 15 00:01:03,970 --> 00:01:07,140 to there and remove the extra return statement. 16 00:01:08,150 --> 00:01:10,910 Let's run our website by pressing F5 and 17 00:01:10,910 --> 00:01:13,850 verify that everything is still working correctly. 18 00:01:13,850 --> 00:01:17,750 Remember even though we replaced our string literal with a content result 19 00:01:17,750 --> 00:01:23,190 object, we still expect our detail action method to return the same content. 20 00:01:23,190 --> 00:01:27,794 Don't forget we need to browse to the URL ComicBooks/Detail. 21 00:01:27,794 --> 00:01:31,320 Great, we [LAUGH] didn't break anything. 22 00:01:31,320 --> 00:01:33,620 Go ahead and close Chrome. 23 00:01:33,620 --> 00:01:37,030 Stop the website and hide the output window. 24 00:01:37,030 --> 00:01:41,620 Content result is just one of the action result types that NVC provides. 25 00:01:41,620 --> 00:01:44,946 Another action result type, redirect result, 26 00:01:44,946 --> 00:01:47,955 is used to redirect the user to another URL. 27 00:01:47,955 --> 00:01:51,411 Let's add an if statement that will redirect the user to the home 28 00:01:51,411 --> 00:01:52,760 page if today is Monday. 29 00:01:54,990 --> 00:02:01,720 We can use the DateTime.Today static property to get a date time for today. 30 00:02:01,720 --> 00:02:04,080 On that object there's 31 00:02:04,080 --> 00:02:08,790 a DayOfWeek property that we can use to check if today is Monday. 32 00:02:08,790 --> 00:02:11,550 To do that we just compare the property value 33 00:02:11,550 --> 00:02:15,090 to the DayOfWeek.Monday enumeration value. 34 00:02:15,090 --> 00:02:18,800 In a later course we'll cover in more detail how to work with dates and 35 00:02:18,800 --> 00:02:20,760 times in C#. 36 00:02:20,760 --> 00:02:23,420 If you'd like to learn more now, see the teacher's note for 37 00:02:23,420 --> 00:02:26,030 links to additional resources. 38 00:02:26,030 --> 00:02:28,150 Okay, inside the if statement, 39 00:02:28,150 --> 00:02:31,270 let's return a new instance of the redirect result class. 40 00:02:33,980 --> 00:02:36,580 In the constructor, let's pass the string literal /, 41 00:02:36,580 --> 00:02:39,170 which represents the root of our website. 42 00:02:40,290 --> 00:02:40,860 Hm. 43 00:02:40,860 --> 00:02:42,730 There's a problem with our code. 44 00:02:42,730 --> 00:02:45,970 If we hover our mouse pointer over the offending code, 45 00:02:45,970 --> 00:02:47,910 we can see the error message. 46 00:02:47,910 --> 00:02:53,898 Cannot implicitly convert type System.Web.Mvc.RedirectResult 47 00:02:53,898 --> 00:02:57,520 to System.Web.Mvc.ContentResult. 48 00:02:57,520 --> 00:03:01,390 Looks like the compiler was unable to implicitly convert 49 00:03:01,390 --> 00:03:05,900 the redirect result type to our methods content result type. 50 00:03:05,900 --> 00:03:10,710 We can try explicitly casting the redirect result object to content result 51 00:03:10,710 --> 00:03:14,510 by typing a set of parenthesis containing the type content result. 52 00:03:17,500 --> 00:03:19,200 But that doesn't work either. 53 00:03:19,200 --> 00:03:24,170 Cannot convert type, RedirectResult to ContentResult. 54 00:03:24,170 --> 00:03:30,090 To resolve this issue, we need a type that isn't common to both of our result types. 55 00:03:30,090 --> 00:03:31,725 Let's go to the definition for 56 00:03:31,725 --> 00:03:36,308 RedirectResult by putting the cursor on the constructor and pressing F12. 57 00:03:36,308 --> 00:03:41,110 Since the RedirectResult class is part of Mvc and 58 00:03:41,110 --> 00:03:43,580 we don't have the source code in our solution, 59 00:03:43,580 --> 00:03:48,610 Visual Studio will only display the basic information for the public class members. 60 00:03:48,610 --> 00:03:51,340 For our purposes, that will work just fine. 61 00:03:51,340 --> 00:03:55,030 We can see that the base class is ActionResult. 62 00:03:55,030 --> 00:03:56,730 Let's go back to our controller and 63 00:03:56,730 --> 00:03:59,360 put our cursor on the ContentResult constructor. 64 00:04:00,370 --> 00:04:03,410 This time let's hold down the alt key when pressing F12. 65 00:04:04,420 --> 00:04:08,420 This allows us to peek the definition in line with our code. 66 00:04:08,420 --> 00:04:10,740 If we scroll up a bit in the peek window, 67 00:04:10,740 --> 00:04:14,430 we can see that the base class is also ActionResult. 68 00:04:14,430 --> 00:04:19,900 So redirect result and content result have a common base class. 69 00:04:19,900 --> 00:04:24,185 Let's close the peek window and change our methods return type to ActionResult. 70 00:04:27,010 --> 00:04:32,434 Problem solved all of the Mvc ActionResult types share the action result base 71 00:04:32,434 --> 00:04:37,943 class because of this action result is almost always used as the return type for 72 00:04:37,943 --> 00:04:39,750 action methods. 73 00:04:39,750 --> 00:04:44,320 Regardless of the specific ActionResult type that is being returned. 74 00:04:44,320 --> 00:04:48,170 Remember the Mvc Controller base class that we're inheriting from? 75 00:04:48,170 --> 00:04:51,060 The Controller base class provides a number of methods 76 00:04:51,060 --> 00:04:53,200 that can help simplify our code. 77 00:04:53,200 --> 00:04:58,060 We can replace our ContentResult object with a call to the Content method, 78 00:04:58,060 --> 00:05:00,170 which accepts a string for the content. 79 00:05:07,743 --> 00:05:12,354 We can also replace our redirect result object with a call to the Redirect 80 00:05:12,354 --> 00:05:15,540 method which accepts a string for the target URL. 81 00:05:18,100 --> 00:05:20,810 Let's run our website and test our changes. 82 00:05:23,848 --> 00:05:26,823 Don't forget to browse to ComicBooks/Detail. 83 00:05:29,395 --> 00:05:32,030 Good, we're still seeing our content. 84 00:05:32,030 --> 00:05:35,820 Next let's stop the website and update our if statement, so 85 00:05:35,820 --> 00:05:38,840 that we can test our call to the Redirect method. 86 00:05:40,630 --> 00:05:42,920 I'm recording this video on a Tuesday. 87 00:05:42,920 --> 00:05:45,190 So I'll update the day of the week to Tuesday. 88 00:05:48,043 --> 00:05:50,970 Restart the website by pressing F5. 89 00:05:50,970 --> 00:05:55,214 When Chrome is finished loading, browse to ComicBooks/detail. 90 00:05:57,510 --> 00:06:01,740 Excellent, notice here in the address bar that we've been redirected 91 00:06:01,740 --> 00:06:03,360 to the root of our website. 92 00:06:03,360 --> 00:06:07,270 Which is still returning a 404 Error, go ahead and stop the website. 93 00:06:09,641 --> 00:06:12,160 If you're using GitHub let's commit our changes. 94 00:06:14,510 --> 00:06:19,761 Enter a commit message of Updated Detail action 95 00:06:19,761 --> 00:06:28,090 method to use action result types and click the Commit All button. 96 00:06:28,090 --> 00:06:30,160 We've done a number of local commits so 97 00:06:30,160 --> 00:06:33,580 now would be a good time to sync with remote server. 98 00:06:33,580 --> 00:06:36,300 Click Sync here in this confirmation message 99 00:06:36,300 --> 00:06:39,010 to navigate to the synchronization panel. 100 00:06:39,010 --> 00:06:43,100 Here's a list of our local commits in the outgoing commit section. 101 00:06:43,100 --> 00:06:45,690 Click the Push link to push these commits to the server. 102 00:06:48,077 --> 00:06:50,490 Okay, we're making progress. 103 00:06:50,490 --> 00:06:53,520 But we're still returning a string for our content. 104 00:06:53,520 --> 00:06:58,270 It's worth noting that we're not limited to returning just a plain string. 105 00:06:58,270 --> 00:07:02,810 We could update our content result to include HTML Markup. 106 00:07:02,810 --> 00:07:07,300 But our controller shouldn't be concerned with the presentation of our content. 107 00:07:07,300 --> 00:07:12,050 As we discussed in an earlier video, controllers are coordinators. 108 00:07:12,050 --> 00:07:13,160 They're responsible for 109 00:07:13,160 --> 00:07:16,750 coordinating the specific actions that need to be performed. 110 00:07:16,750 --> 00:07:20,550 In order to return a response for that user request. 111 00:07:20,550 --> 00:07:23,970 Presenting content is a job better suited for a view. 112 00:07:25,030 --> 00:07:27,820 So, how do we return a view? 113 00:07:27,820 --> 00:07:30,660 In the next section, we'll add our first view. 114 00:07:30,660 --> 00:07:31,160 See you see then.