Razor Pages is a new lightweight version of MVC pages. In this section I show you how you use Razor Pages.
- [Instructor] Another brand new feature in ASP.NET Core 2.0 are Razor Pages and these are designed for creating page-focused scenarios that contain much less overhead than traditional MVC views. They're enabled alongside MVC. As we've stated many times here out of this course, it's all one framework. There isn't a separate framework for Web API, MVC, or Web Pages. And the framework, that is the ASP.NET Core framework, looks for Razor Pages in the Pages folder by default.
So, Views go into Views folder, Controllers, Controllers, Pages, Pages. You see how it goes. One major difference between Razor Pages and MVC views is that Razor Pages have to have an @page directive at the top. And then a Razor Page operates as an action. The request don't go through a controller. They're actually routed based on the folder hierarchy under Pages in the name of the Razor Page.
You also have code behind files you can use to put C# code in there. And they support model binding, Tag Helpers, HTML Helpers, dependency injection, and quite a lot actually. So, let's look at these in code. And what I've done for the SpyStore 20 project is I have moved the About and the Contact Us away from the HomeController because they really don't do any heavy lifting whatsoever and made them into Razor Pages.
So, you'll see two changes on the Contact view as a Razor Page, and that is on line one and it has to be the first line on the page is the @page directive. Then I'm specifying that there is a model and that gives us access into the @Model Razor code so we can get into whatever is in the code behind. If we look at our directory structure, we have Pages, Home, so this is very similar to how Views, Home, so it's a Home controller, About action and Contact action.
And we can use the ViewImports and ViewStart just like we can in the Views folder. And it's important to point out that the ViewImports hasn't changed but we need it in the Pages directory. And ViewStart, when we specify the Layout, we don't have to specify a directory structure. It will look in the shared views as well. So, let's look at the code behind for the Contact Razor Page and the naming convention is ContactModel.
It derives from PageModel. And then we can have public properties. We can do injection into here. We can do whatever we need to do. And when the page is called, the OnGet is what gets executed, presuming that it is a get request. Let's look at the About page, and what I have done is I've add some Settings messages here in addition to adding the page directive and the model directive.
And in the model, I am injecting and I have to use FromServices because it is not constructor injection, but in IOptionsSnapshot Custom Settings, just to verify that we can do injection into a Razor Page. And then I'm just setting those silly settings to values on the PageModel so that we can display them on the page. So, let's run this. I'm actually going to take it out of Docker, so it runs faster.
So again, just switch to the ASP.NET Core project, Set as Startup Project, and I'm going to run in Kestrel. That's fine. I'm going to execute. It will load up our application. Here we are at the Home page. Let's go to the Contact page. We see that it is still routed. Home/Contact executes the Home page. Just to prove there is no magic person behind the curtain, if we go into the HomeController, we can see that the About and Contact action methods have been commented out.
So, we go back into our application and I don't have a menu for this 'cause I ran out of room, but let's just type About and we will see that Settings 1 and Setting 2 do show up, which means our IOptionsSnapshot was injected in and we're verifying that Razor Pages do support a lot of features that MVC views support. They're just lighter weight.
- Running and debugging ASP.NET Core applications
- Pros and cons of migrating existing applications to ASP.NET Core.
- Built-in dependency injection
- Environment awareness and app configuration
- Web host configuration and SSL
- View components invoked as tag helpers
- Configuration and logging
- Using Razor Pages