Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we have our project, let’s add the first controller for our Comic Book Gallery website.
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/v1.5 -b adding-a-controller
Controller Scaffolding
In this video, we added a controller to our project by adding a C# class using the “Add > Class...” menu item. Using this method allowed us to understand what makes a C# class an ASP.NET MVC controller. While this method works, it’s not the typical way that developers add controllers to their ASP.NET MVC projects.
Visual Studio provides a feature called “scaffolding” that can be used to quickly and easily add items to a project. You can use scaffolding to add a controller to an ASP.NET MVC project by right-clicking on the “Controllers” folder and selecting the “Add > Controller…” menu item. That’ll open up the “Add Scaffold” dialog, which will present you with a list of controller templates to choose from.
For more information on how to add a controller to an ASP.NET MVC project use Visual Studio scaffolding see http://www.asp.net/mvc/overview/getting-started/introduction/adding-a-controller.
Additional Learning
For more information on how to perform quick actions with light bulbs in Visual Studio, checkout out this MSDN page.
For more information about classes, access modifiers, and inheritance, check out these pages on MSDN.
Keyboard Shortcuts
-
CTRL+SHIFT+B
- Build solution
-
0:00
[MUSIC]
-
0:04
Welcome back.
-
0:06
We just created our new project, but
-
0:08
when we tried running our website, we encountered in error.
-
0:11
We were able to determine that the error occurred,
-
0:14
because our website didn't have any controllers reviews.
-
0:18
We're building a comic book gallery website.
-
0:21
One of the main features will be the ability to view the details for
-
0:25
a specific comic book.
-
0:27
Let's call this our comic book detail page.
-
0:30
That seems like a great place to start.
-
0:33
So let's create a controller to handle the request for that page.
-
0:38
Looking at our project here in the solution explorer panel,
-
0:41
we can see that it contains a folder named controllers.
-
0:45
While it's not absolutely necessary,
-
0:47
it's a common convention to put all of your websites controllers in this folder.
-
0:51
And remember following conventions often makes it easier for
-
0:55
other developers to work on your code, which is super helpful.
-
0:58
Right-click on the Controllers folder and select the Add > Class menu item.
-
1:05
Let's name our class ComicBooksController.
-
1:09
Adding the suffix controller is more than a convention, it's a requirement.
-
1:13
Without it NVC wouldn't be able to distinguish our website's controllers
-
1:18
from other classes in our project.
-
1:20
Click the Add button to finish adding the class.
-
1:24
Okay, here's our class.
-
1:27
Let's start with talking about the public access modifier.
-
1:30
Visual Studio will include the public access modifier by default
-
1:34
when adding new classes to a project.
-
1:37
This makes our class accessible to code outside of our project.
-
1:41
You might be wondering if our class needs to be public.
-
1:44
For now, let's leave it as it is.
-
1:46
We will try an experiment in the next video to answer that question.
-
1:51
We need to make a couple of changes to our controller before it is usable.
-
1:55
To start with,
-
1:56
we need to update our class to inherit from the MVC controller base class.
-
2:00
To do that,
-
2:01
type a colon at the end of the class name followed by the name of the base class.
-
2:07
Before we go any further, let's build our project.
-
2:10
There are multiple ways to do this, of course.
-
2:13
You can click on the build build solution menu item, or
-
2:18
you can right click on the solution in the solution explorer panel and
-
2:22
select the build solution menu item.
-
2:24
Notice that to the right of this menu item,
-
2:26
you can see the keyboard shortcut for this command, control Shift B.
-
2:31
That's my favorite option as it keeps your hands on the keyboard.
-
2:34
Pressing control Shift B kicks off the build process.
-
2:38
Visual studio opens the output window, so that we can monitor
-
2:41
the progress of the build and see the results upon its completion.
-
2:46
Looks like we got an error.
-
2:47
The type or namespace name controller could not be found.
-
2:51
Are you missing a using directive or an assembly reference?
-
2:55
Luckily this error is telling us exactly what the problem is.
-
2:59
We need to add a using directive.
-
3:01
We could also see the Visual Studio is underlying the controller base class name
-
3:06
in an angry red squiggle.
-
3:08
If we hover our mouse pointer over the offending code,
-
3:11
Visual Studio will display a pop up containing information about the air.
-
3:15
Notice to the left of the pop up, there's a light bulb icon.
-
3:18
If we click on it,
-
3:20
a list of quick actions are displayed that we can take to resolve this error.
-
3:24
The first item in the list is using system.web.nvc.
-
3:28
And to the right, we can see a small visual preview of that change that
-
3:33
will be made if we select that action.
-
3:36
How cool is that?
-
3:37
Go ahead and click on the first item.
-
3:40
The using directive has been added,
-
3:42
the red squiggle has gone away, and our project is building again.
-
3:47
Adding using directives is a common activity, and
-
3:50
this Visual Studio quick action makes the process quick and painless.
-
3:55
If you're using GitHub, let's finish up by committing our changes.
-
3:58
Switch back to the team explore panel and
-
4:01
click the Home icon, if you're not already on the home panel.
-
4:05
Then under the project's section click on the changes button.
-
4:09
Enter a commit message of,
-
4:10
added comic books controller, and click the commit all button.
-
4:16
If Visual Studio prompts you to save the ComicBookGallery.sln file,
-
4:21
go ahead and click the Yes button to confirm the action.
-
4:25
In the next video, we'll continue with building out our controller.
-
4:28
See you then.
You need to sign up for Treehouse in order to download course files.
Sign up