reading-notes

Razor Pages

Introduction to Razor Pages

Razor Pages are introduced as a new way to build web applications in ASP.NET Core 2. They are sometimes seen as a revival of an old concept called WebMatrix.

Why Razor Pages?

Razor Pages are presented as a more beginner-friendly and lightweight alternative to MVC for web development. They are considered suitable for smaller projects where the complexity of controllers and models is not necessary.

Creating Razor Pages Application

Visual Studio 2017 Preview 2 provides a template for creating Razor Pages applications. The application structure resembles MVC but lacks separate folders for controllers and views.

Application Structure

The project structure includes a “Pages” folder containing Razor views, referred to as “pages.” Razor Pages share the same Program and Startup classes as MVC applications.

Separation of Logic and Presentation

Razor Pages allow for code-behind files named as “PageName.cshtml.cs,” where the class is called the “page model.” This separation enables developers to maintain logic separately from the presentation.

Exploring a Razor Page

A Razor page includes a @page directive to identify it as a Razor page. Developers can specify a model, similar to a view model, which blends controller and model concepts. Handler methods like OnGet() are used for GET requests and have asynchronous counterparts.

Razor Page with No Code-Behind

Demonstrates how a Razor page can function without a separate code-behind file. In this case, methods and properties are defined within the @functions section within the page itself.

Wrapping Up

Reflects on the usage of Razor Pages as a lightweight option for beginners and simple in-house web projects. Questions whether Razor Pages will have distinct use-cases compared to the well-established MVC framework.

comparison between MVC and Razor Pages

### MVC:

Razor Pages:

This comparison highlights the differences between MVC and Razor Pages, including how they handle requests, where code is placed, where markup resides, how data is displayed, and their respective folder structures. It’s essential to consider these distinctions when choosing the appropriate framework for your ASP.NET Core project.