Join Jess Chadwick for an in-depth discussion in this video Creating a new Razor Pages application, part of ASP.NET Core: Razor Pages.
- [Instructor] Now that I've explained what Razor Pages is and how to get your development environment set up to build Razor Pages applications, let's go ahead and create one. To begin, open Visual Studio and select File > New Project. Once in the New Project dialog, select the .NET Core category and choose the ASP.NET Core Web Application. Then give your project a name and tell Visual Studio where on your hard drive you want it to live.
Since I'll be building the Topsy Turvy website, I'll call my application TopsyTurvyCakes. When you're ready, hit OK to go to the next step, which is choose the template you'd like to use to create your new ASP.NET Core web application. Once you get to this step, be sure to select the ASP.NET Core 2.0 option or higher. As of this recording, these are the templates that Microsoft makes available by default, but future updates may change this list, so don't worry if yours includes or doesn't include the templates shown here.
There are three templates from this list that are good choices for creating an application that uses Razor Pages. First, there are the two web application templates. Though they both share the same name, the fundamental difference between the two of them is the way they are organized. The Web Application (Model View Controller) template, otherwise known as the MVC template, generates an application that uses the MVC architecture approach, which uses controllers to process requests and renders views, whereas the plain old Web Application template generates those same features as Razor Pages.
You can implement a Razor Pages application in either of the two web application templates, as they both include everything you'll need. It all just depends on whether you would like to leverage the model view controller pattern in your application. The other good candidate for starting a Razor Pages application is the Empty template as it generates a bare-bone ASP.NET Core application with a minimal amount of boiler-plate code for folks who want to build everything from scratch or say folks who want to learn parts of a framework bit by bit.
After you complete this course and begin building your own Razor Pages applications, you'll probably want to choose the Web Application template as it generates a lot of useful code to help you get started quickly. However, since I'm building this application for the purpose of teaching framework features, I'm going to choose the bare-bones Empty template, so that I can start out with a clean slate that lets me introduce you to each Razor Pages features out at a time.
After I've made my choice, I'll hit the OK button to have Visual Studio generate the project for me. Once the basic ASP.NET Core project is created, there are a few things that I need to do in order to prepare it to serve Razor Pages properly. The first thing I need to do is create a folder in the root of the project, named Pages. The name of this folder is important, as this is where the framework will look for Razor Pages by default.
Then, I'll add the ASP.NET Core MVC middleware by opening up the startup class, scrolling to the end, and replacing the app.Run call with a call to UseMvcWithDefaultRoute. Likewise, I'll need to register the MVC services with the dependency injection framework by calling services.AddMvc in the ConfigureServices method above.
Though it may seem odd that I'm adding references to the ASP.NET Core MVC framework when I never actually intend to use the MVC pattern, keep in mind that Razor Pages is actually a superset of ASP.NET Core MVC, so these references also bring in the Razor Pages feature as well, and if you're not sure why I added that call to services.AddMvc, or what dependency injection is, don't worry. I'll give you a quick primer on that later in this course.
And finally, I'm going to do one last thing, and that's to copy the Models folder from the exercise files that contains the recipe and recipe service classes that I described in the previous chapter, which I'll use to save and retrieve the recipe data for the site. Keep in mind that these model files are just regular C# classes that I'll use to get access to my recipe data, and have nothing to do with the Razor Pages framework at all and with these few things in place, I'm now ready to start adding some Razor Pages.
- Creating a new application
- Setting up pages
- Rendering dynamic content
- Reusing markup with layouts
- Increasing the maintainability of pages
- Processing data
- Validating input
- Securing an application