Controllers and Views4:09 with James Churchill
In this video, we'll take a look at MVC controllers and views, which have changed from previous versions of MVC, but not to the same degree as other parts of ASP.NET.
Our home controller class inherits from MVC's controller base class 0:00 though doing so is now optional. 0:04 You can now create POCOC, plain old seal our object controllers. 0:07 While it's nice to have that flexibility, using that approach 0:12 would mean that you wouldn't have access to convenience methods. 0:16 Like the view method that we're using in our index action method. 0:19 The only thing that is different here from previous versions of MVC is that 0:23 our action method return type is now the interface IActionResult instead 0:27 of the concrete type ActionResult. 0:32 Here's the view for the About page. 0:39 Obviously, this is a very simple view. 0:41 But as you can see, this looks just like an ordinary MVC view. 0:44 Let's add a new controller and view to our project. 0:49 I also like the MVC controller class template. 0:59 Change the name to ProductsController. 1:02 And click Add. 1:10 Now let's add a view for the index action method. 1:15 In this version of the Visual Studio ASP.NET Core tooling, 1:19 there isn't a right-click shortcut menu item for adding a view. 1:22 First, I'll add a folder to the Views folder named products. 1:30 Then I'll right-click on the Products folder, and select Add, New item. 1:45 Select the MVC View Page template, and click Add. 1:55 Let's add some content to our view,. 2:01 First, I'll set the ViewData Title key to the string literal Products. 2:05 Then, I'll add an H2 element and set its contents to the view data title key value. 2:20 Now that we have our new controller in view, 2:26 let's update the main menu with a link to our new page. 2:28 Here's our layout page, I'll scroll down a bit to the markup for the main menu. 2:37 Here we are, 2:47 you'll notice right away that the markup doesn't look like you'd expect it to. 2:47 Instead of using calls to the action link HTML helper method 2:51 to create the main menu hyperlinks, we're just using plain old anchor elements. 2:55 The asp-area, asp-controller and 3:00 asp-action element attributes are a new MVC feature called take helpers. 3:04 In the next video, we'll take a closer look at what take helpers are and 3:10 how we can use them. 3:14 For now, let's just copy the first line item and 3:15 update it to be a link to our new products page. 3:18 Change the controller to products, action can stay as index, 3:25 and set the anchor element in our content to Products. 3:30 Now let's run our app and test our new products page. 3:35 Here's our new products menu item. 3:49 And clicking on it takes us to our new Products page. 3:51 As you've seen in this video, controllers and views, for 3:55 the most part, look and feel like they did in the previous version of MVC. 3:58 This allows you to leverage your existing MVC skills when working with ASP.NET Core. 4:03
You need to sign up for Treehouse in order to download course files.Sign up