简体   繁体   中英

How to extend Razor Pages

I would like to create an ASP.NET Core 2.2 project using Razor Pages.

My objective is to create a project that contains two categories of Razor Page:

  • Standard category: this category contains all Razor Pages that will be shared across multiple project
  • Custom category: this category contains all Razor Pages that are relative to the project.

Up to here is all ok, I create two subfolder inside "Pages" directory: Standard folder, Custom folder. I populate standard and custom folders with all razor page I need in every project, where all the razor pages contained inside Standard folderz are the same in all my project.

For example

Project A:

  • /Pages
    • /Standard
      • /SectionA
        • PageA
        • PageB
    • /Custom
      • /SectionE
        • PageD

Project B:

  • /Pages
    • /Standard
      • /SectionA
        • PageA
        • PageB
    • /Custom
      • /SectionP
        • PageK

Project C:

  • /Pages
    • /Standard
      • /SectionA
        • PageA
        • PageB
    • /Custom
      • /SectionV
        • PageJ

Here born my problem: I would implement the possibility, inside every project, to extend a standard razor page, maybe to rewrite some endpoint (for example I want to change the behaviour of "onGet" method) or create a new one (like "onGetMyCustomGet").

I know that the simpliest solution is to change the code inside the standard razor page, but I want to avoid this because standard pages can be replaced with new versions. So, Standard Pages must be project-indipendent and open to extension.

My idea is to create a "fake" Razor page, inside "/Custom" category that extend the standard razor PageModel and override all methods I need to reimplement.

The problem is: how can I return the standard razor page cshtml instead the fakeRazorPage cshtml? Is it possible?

Here you will find a sample project https://github.com/Blackleones/RazorPageResearch feel free to send pull request if you want collaborate.

Other ideas are accepted if there are not solutions

You don't have to copy & paste the source code of Standard Pages everywhere. Simply create a separate Razor Class Library project and reference it in your Project A /Project B / Project C, and then you could custom anything as you like.

Let's say you create a new project RazorResearch.Core as the "standard" Page:

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.2" />
  </ItemGroup>
</Project>

Here's the directory structure of the RazorResearch.Core project:

└───Pages/
    └───SectionA/
        └───PageA.cshtml
        └───PageA.cshtml.cs
        └───PageB.cshtml
        └───PageB.cshtml.cs
└───RazorResearch.Core.csproj

And now reference this RazorLib :

dotnet add reference <path-to-the-RazorResearch.Core.csproj>

Now you could custom any page as you like. For example, to custom the standard SectionA/PageA.cshtml , simply create a new Pages/SectionA/PageA.cshtml file.

└───Pages/
    └───SectionA/
        └───PageA.cshtml
        └───PageA.cshtml.cs
    └───SectionE/
        └───PageD.cshtml
        └───PageD.cshtml.cs
    └───Shared/
    └─── ...
└───WebApplication3.csproj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM