1 00:00:00,520 --> 00:00:04,480 The next change that we need to make to our controller is to add a method to it. 2 00:00:04,480 --> 00:00:08,300 Well, not just any method, an action method. 3 00:00:08,300 --> 00:00:10,010 What is an action method? 4 00:00:10,010 --> 00:00:13,610 Every public method on a controller is an action method. 5 00:00:13,610 --> 00:00:14,890 They're responsible for 6 00:00:14,890 --> 00:00:20,620 performing any actions that are required to prepare a response for a request. 7 00:00:20,620 --> 00:00:24,440 Let's add a public method to our controller using the name Detail. 8 00:00:27,190 --> 00:00:28,770 If we want to return content for 9 00:00:28,770 --> 00:00:33,950 our response, we need our action method to have a return type other than void. 10 00:00:33,950 --> 00:00:38,110 For now let's use the string data type for our return type and 11 00:00:38,110 --> 00:00:45,775 return the string literal, Hello from the comic books controller. 12 00:00:45,775 --> 00:00:52,040 Okay, now that we have our controller set up, let's try again to run our website. 13 00:00:52,040 --> 00:00:56,550 Visual Studio will build the project, open up your selected browser, which for 14 00:00:56,550 --> 00:00:59,430 me is Google Chrome, and browse to the root of the website. 15 00:01:00,680 --> 00:01:04,570 Bummer, we still get the same 404 error. 16 00:01:04,570 --> 00:01:07,490 In order to understand why we're still getting this error, 17 00:01:07,490 --> 00:01:12,800 we need to talk about how MVC associates URLs with controller actions. 18 00:01:12,800 --> 00:01:13,690 But, for now, 19 00:01:13,690 --> 00:01:18,210 I'm just going to show how to resolve this issue without giving you an explanation. 20 00:01:18,210 --> 00:01:22,270 Don't worry, we'll cover all of the details in the next video. 21 00:01:22,270 --> 00:01:24,500 Let's click into the address bar and 22 00:01:24,500 --> 00:01:30,940 type the URL /comicbooks/detail and press Enter. 23 00:01:32,010 --> 00:01:34,760 And now, we can see our content. 24 00:01:34,760 --> 00:01:38,570 Before we end this video, let's try an experiment to see if the public 25 00:01:38,570 --> 00:01:43,580 access modifier is required on either the controller class or the action method. 26 00:01:43,580 --> 00:01:46,170 Let's close the browser and stop the website. 27 00:01:47,590 --> 00:01:52,690 First, let's remove the public access modifier on the class, 28 00:01:52,690 --> 00:01:55,040 save the file, and rerun our website. 29 00:01:57,160 --> 00:02:02,186 Remember we need to browse to the URL /comic books/detail. 30 00:02:03,420 --> 00:02:04,660 That's interesting. 31 00:02:04,660 --> 00:02:08,800 Now we get a 404 error instead of the expected content. 32 00:02:08,800 --> 00:02:13,600 Let's switch back to Visual Studio, stop the website, and 33 00:02:13,600 --> 00:02:17,300 add the public access modifier back to the class. 34 00:02:17,300 --> 00:02:22,610 When we rerun the website, Visual Studio will open another tab in Chrome. 35 00:02:22,610 --> 00:02:26,460 Go ahead and close that tab and refresh the previously open tab. 36 00:02:27,470 --> 00:02:30,320 Great, now our content is back. 37 00:02:30,320 --> 00:02:32,570 So what does this tell us? 38 00:02:32,570 --> 00:02:35,520 Controller classes need to be public. 39 00:02:35,520 --> 00:02:39,820 Otherwise NVC won't be able to find and use them. 40 00:02:39,820 --> 00:02:42,950 We could run the same experiment with our action method. 41 00:02:42,950 --> 00:02:44,370 But I'll save us some time. 42 00:02:44,370 --> 00:02:48,260 Action methods need to be public too, not surprisingly for 43 00:02:48,260 --> 00:02:51,580 the same reason why we had to make the controller class public. 44 00:02:51,580 --> 00:02:56,870 If our action methods aren't public, NVC won't be able to find and call them. 45 00:02:56,870 --> 00:02:59,402 If you're using GitHub, let's commit our changes. 46 00:02:59,402 --> 00:03:03,912 Enter a commit message of Added Detail 47 00:03:03,912 --> 00:03:07,690 action method and click the Commit All button. 48 00:03:09,040 --> 00:03:11,840 In the next video let's pull back the covers and 49 00:03:11,840 --> 00:03:16,450 take a look at how NVC associates URLs with controller actions.