Learn

Wouldn’t it be great if you could get started creating a web application by just running a few commands? Well, that’s possible now with Razor Pages!!

When it comes to building web applications with ASP.NET we’re usually able to take two approaches: we can take an MVC (Model-View-Controller) approach, or more recently, use Razor Pages. Whether one approach is better than the other really depends on what you’re building and its complexity, but let’s look at some reasons why we should use Razor Pages.

Compared to Razor Pages, the MVC pattern uses a more complex folder structure and requires a deeper understanding of more web framework concepts in order to get started.

With Razor Pages, we don’t have to worry about Controllers, Actions, and Views the same way we would in an MVC app. Now, we use what we call Pages and PageModels. Everything related to a specific “Page” is in one place and PageModels takes care of the processing logic for the said page (We will go more in-depth into these later in the course). This makes applications very cohesive and requires fewer additional concepts to learn. Throughout this course, we will be referring to Pages as “view pages” and they will have a cshtml extension to their filename (i.e. Home.cshtml). PageModels will be referred to as a “model page” and will have a cshtml.cs extension to their filename (i.e. Home.cshtml.cs).

Razor Pages also has a much more organized file structure; instead of having a whole chain of events in different files when working with MVC, we now simply have a Razor View file, where we can write C# code together with HTML, and a code-behind file (the PageModel) to handle the logic of what the user interacts with.

What this essentially means is that now, there is a direct link between the URL of a web page and the physical file location of that page on the server.

The URL https://LearnRazorPages.com/Home/Welcome

will use the Welcome.cshtml file located in the /Home folder of the root /Pages folder.

RazorPagesURL

RazorPageDirectory

In essence, Razor is a view templating engine that’s currently supported in ASP.NET. With Razor, we’re able to create dynamic web pages and write C# and HTML together using Razor syntax.

Instructions

We’ve provided you with a demo app so you can explore how the folder structure of a Razor Pages app looks. Click Run to launch the application.

  • Take a look at Index.cshtml. This is what we call a “view page”, can you find what parts of the markup is actually C# code?

  • Navigate to Index.cshtml.cs. This is a “model page”, can you figure out how it’s related to Index.cshtml? Given what you know about HTTP requests, when do you think the OnGet() method is called?

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?