.net-corerazor-pages

Split Razor Pages into sub projects to make it modular


I'm trying to find a way to break down a razor pages project into multiple projects to make it modular.

The idea is that I want to isolate the functionality related to a module into a different project instead of having a single project that manages all. Basically, I follow a vertical slice clean architecture with my API projects and I want to accomplish the same with razor pages.

The way I imagine the structure is as follows:

enter image description here enter image description here

A core razor pages library project where that will contain the layout, any shared partial views and resources.

A razor pages library project for each module that will contain the files related to the module.

The main razor pages web project that will reference each module project and will contain some general views (eg privacy policy, etc.) and will produce the end result.

I have a non-working example located here https://github.com/panosru/Playground/tree/master/C%23/ModularRazorPages just to illustrate the structure I have in my mind.

The issue is that I cannot find any resources online that would guide me on how to split a razor pages project into modules and I'm not sure if that is even possible.

Has anyone tried that before, if yes, can you show me an example of something that would help?


Solution

  • After some more digging I found this article, which by coincidence was posted on 18th January 2021, almost a month after I asked my question.

    By following this article it actually provided me with the solution I was seeking and now I can easily have my razor pages split between projects to have separation of concerns and follow vertical slice architecture.

    The trick was to use AddRazorSupportForMvc directive in csproj and Microsoft.AspNetCore.App as a FrameworkReference.

    Basically, the bare minimum csproj to make it work would be:

    <Project Sdk="Microsoft.NET.Sdk.Razor">
     
      <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
      </PropertyGroup>
     
      <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
      </ItemGroup>
       
    </Project>
    

    Still, I would recommend reading the article, it is interesting.