Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The next change that we need to make to our controller, is to add a method to it. Well, not just *any* method—an “action” method.
Follow Along
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.2 -b adding-an-action-to-a-controller
Additional Learning
For more information about methods and access modifiers check out these pages on MSDN.
Keyboard Shortcuts
-
F5
- Start debugging (i.e. build and run your website/project) -
CTRL+S
- Save Current File
The next change that we need to make to
our controller is to add a method to it.
0:00
Well, not just any method,
an action method.
0:04
What is an action method?
0:08
Every public method on
a controller is an action method.
0:10
They're responsible for
0:13
performing any actions that are required
to prepare a response for a request.
0:14
Let's add a public method to our
controller using the name Detail.
0:20
If we want to return content for
0:27
our response, we need our action method
to have a return type other than void.
0:28
For now let's use the string data type for
our return type and
0:33
return the string literal,
Hello from the comic books controller.
0:38
Okay, now that we have our controller set
up, let's try again to run our website.
0:45
Visual Studio will build the project,
open up your selected browser, which for
0:52
me is Google Chrome, and
browse to the root of the website.
0:56
Bummer, we still get the same 404 error.
1:00
In order to understand why
we're still getting this error,
1:04
we need to talk about how MVC associates
URLs with controller actions.
1:07
But, for now,
1:12
I'm just going to show how to resolve this
issue without giving you an explanation.
1:13
Don't worry, we'll cover all of
the details in the next video.
1:18
Let's click into the address bar and
1:22
type the URL /comicbooks/detail and
press Enter.
1:24
And now, we can see our content.
1:32
Before we end this video, let's try
an experiment to see if the public
1:34
access modifier is required on either
the controller class or the action method.
1:38
Let's close the browser and
stop the website.
1:43
First, let's remove the public
access modifier on the class,
1:47
save the file, and rerun our website.
1:52
Remember we need to browse to
the URL /comic books/detail.
1:57
That's interesting.
2:03
Now we get a 404 error instead
of the expected content.
2:04
Let's switch back to Visual Studio,
stop the website, and
2:08
add the public access
modifier back to the class.
2:13
When we rerun the website, Visual Studio
will open another tab in Chrome.
2:17
Go ahead and close that tab and
refresh the previously open tab.
2:22
Great, now our content is back.
2:27
So what does this tell us?
2:30
Controller classes need to be public.
2:32
Otherwise NVC won't be able to find and
use them.
2:35
We could run the same experiment
with our action method.
2:39
But I'll save us some time.
2:42
Action methods need to be public too,
not surprisingly for
2:44
the same reason why we had to
make the controller class public.
2:48
If our action methods aren't public,
NVC won't be able to find and call them.
2:51
If you're using GitHub,
let's commit our changes.
2:56
Enter a commit message of Added Detail
2:59
action method and
click the Commit All button.
3:03
In the next video let's
pull back the covers and
3:09
take a look at how NVC associates
URLs with controller actions.
3:11
You need to sign up for Treehouse in order to download course files.
Sign up