23. Using Razor Pages Flashcards
What are Razor Pages?
Razor Pages are a simplified way of generating HTML responses.
Razor Pages are interesting because of the developer experience and not the way they are implemented.
For many features, a single action method will be used to handle a wide range of requests, all of which are dealt with using the same view. Razor Pages offer a more focused approach that ties together markup and C# code, sacrificing flexibility for focus.
Why are they useful?
The simplicity of Razor Pages means you can start getting results sooner than with the MVC Framework, which can require a relatively complex preparation process. Razor Pages are also easier for less experienced web developers to understand because the relationship between the code and content is more obvious.
How are they used?
Razor Pages associate a single view with the class that provides it with features and uses a file-based routing system to match URLs.
Are there any pitfalls or limitations?
Razor Pages are less flexible than the MVC Framework, which makes them unsuitable for complex applications. Razor Pages can be used only to generate HTML responses and cannot be used to create RESTful web services.
Are there any alternatives?
The MVC Framework’s approach of controllers and views can be used instead of Razor Pages.
How does the MVC Framework work?
The MVC Framework solves every problem in the same way: a controller defines action methods that select views to produce responses. It is a solution that works because it is so flexible: the controller can define multiple action methods that respond to different requests, the action method can decide which view will be used as the request is being processed, and the view can depend on private or shared partial views to produce its response.
What does the AddRazorPages method do? (in the Startup class)
The AddRazorPages method sets up the service that is required to use Razor Pages.
What does the MapRazorPages method do? (Startup class)
The MapRazorPages method creates the routing configuration that matches URLs to pages.
Difference between Pages and the MVC Framework
Razor Pages use the Razor syntax and the same CSHTML file extension. But there are some important differences.
The @page directive must be the first thing in a Razor Page, which ensures that the file is not mistaken for a view associated with a controller. But the most important difference is that the @functions directive is used to define the C# code that supports the Razor content in the same file.
How does the URL routing convention work?
URL routing for Razor Pages is based on the file name and location, relative to the Pages folder. The file named Index.cshtml, in the Pages folder, means that it will handle requests for the /index. The routing convention can be overridden, but, by default, it is the location of the Razor Page file that determines the URLs that it responds to.
How does the page model work?
In a Razor Page, the @model directive is used to select a page model class, rather than identifying the type of the object provided by an action method. The page model is defined within the @functions directive and is derived from the PageModel class. When the Razor Page is selected to handle an HTTP request, a new instance of the page model class is created, and dependency injection is used to resolve any dependencies that have been declared using constructor parameters. The IndexModel class declares a dependency on the DataContext service, which allows it to access the data in the database. After the page model object has been created, a handler method is invoked. The name of the handler method is On, followed by the HTTP method for the request so that the OnGet method is invoked when the Razor Page is selected to handle an HTTP GET request. Handler methods can be asynchronous, in which case a GET request will invoke the OnGetAsync method, which is the method implemented by the IndexModel class. Values for the handler method parameters are obtained from the HTTP request using the model binding process. The OnGetAsync method receives the value for its id parameters from the model binder, which it uses to query the database and assign the result to its Product property.
How does the Page view work?
Razor Pages use the same mix of HTML fragments and code expressions to generate content, which defines the view presented to the user. The page model’s methods and properties are accessible in the Razor Page through the @Model expression. The Product property defined by the IndexModel class is used to set the content of an HTML element, like this:
…
<div>@Model.Product.Name</div>
…
The @Model expression returns an IndexModel object, and this expression reads the Name property of the object returned by the Product property.
How does the Generated C# Class work?
Behind the scenes, Razor Pages are transformed into C# classes, just like regular Razor views. Razor Pages rely on the same features used by the MVC Framework. The HTML fragments and view expressions are transformed into calls to the WriteLiteral and Write methods.
How does razor pages routing work?
Razor Pages rely on the location of the CSHTML file for routing.
Example:
http://localhost:5000/suppliers/list
How do you specify a routing pattern?
Using the folder and file structure to perform routing means there are no segment variables for the model binding process to use. Instead, values for the request handler methods are obtained from the URL query string, which you can see by using a browser to request http://localhost:5000/index?id=2. The query string provides a parameter named id, which the model binding process uses to satisfy the id parameter defined by the OnGetAsync method in the Index Razor Page.
So the query string parameter in the request URL is used to provide the id argument when the OnGetAsync method is invoked, which is used to query the database for a product.
The @page directive can be used with a routing pattern like,
@page “{id:long?}”
which allows segment variables to be defined as http://localhost:5000/index/4
The route pattern adds an optional segment variable named id, which is constrained so that it will match only those segments that can be parsed to a long value.
The @page directive can also be used to override the file-based routing convention for a Razor Page like this:
@page “/lists/suppliers”
The directive changes the route for the List page so that it matches URLs whose path is /lists/suppliers.
http://localhost:5000/lists/suppliers