ASP.NET CORE

Razor Pages Conventions

posted by tungnt 16/09/2017 0 comments

The Razor Pages a great piece of technology that shine for the first time in ASP.NET Core 2.0. It brings a new programming model, that is suited for page focus web applications.

One of the cool things that I like about it is the page conventions, which I’m trying to explore today. There are five conventions that Razor Pages have, basically they are an extension methods for PageConventionCollection class, these page conventions are widely used in almost Razor Pages web applications:

1- AllowAnonymousToPage

This convention will allow the anonymous users to access a specific page in your web application.

services.AddMvc()
 .AddRazorPagesOptions(options =>
 {
    options.Conventions.AllowAnonymousToPage("/Pages/Admin/Login");
 });

2- AllowAnonymousToFolder

This convention will allow the anonymous users to access a specific folder in your web application.

services.AddMvc()
 .AddRazorPagesOptions(options =>
 {
   options.Conventions.AllowAnonymousToFolder("/Pages/SharedFolder");
 });

3- AuthorizePage

This convention will authorize the access to a specific page for an authorized users or a certain security policy.

services.AddMvc()
 .AddRazorPagesOptions(options =>
 {
   options.Conventions.AuthorizePage("/Pages/SecurePage");
   options.Conventions.AuthorizePage("/Pages/OffersPage", "SubscriberPolicy");
 });

4- AuthorizeFolder

This convention will authorize the access to a specific folder for an authorized users or a certain security policy.

services.AddMvc()
 .AddRazorPagesOptions(options =>
 {
   options.Conventions.AuthorizeFolder("/Pages/Admin");
   options.Conventions.AuthorizeFolder("/Pages/Offers", "SubscriberPolicy");
 });

5- AddPageRoute

This convention will allow you to add a specified route to a page.

services.AddMvc()
 .AddRazorPagesOptions(options =>
 {
   options.Conventions.AddPageRoute("/Post/Archive.cshtml", "Post/{year}/{month}/{day}")
 });
Bình luận
1731 Total Views 6 Views Today