Using Razor class libraries
Everything related to a Razor page can be compiled into a class library for easier reuse. With .NET Core 3.0 and later this can now include static files. A website can either use the Razor page's view as defined in the class library or override it.
- Create a subfolder in
PracticalApps
namedNorthwindEmployees
. - In Visual Studio Code, add the
NorthwindEmployees
folder to thePracticalApps
workspace. - Navigate to Terminal | New Terminal and select
NorthwindEmployees
. - In Terminal, enter the following command to create a Razor Class Library project:
dotnet new razorclasslib
- Edit
NorthwindEmployees.csproj
, and add a reference to theNorthwindContextLib
project, as shown in the following markup:<ItemGroup> <ProjectReference Include= "..\NorthwindContextLib\NorthwindContextLib.csproj" /> </ItemGroup>
- In Terminal, enter the following command to restore packages and compile the project:
dotnet build
...