Using Razor Class Libraries
Explore how to create and implement Razor Class Libraries to organize and reuse Razor Pages, views, and static files across ASP.NET Core projects. Understand how to set up class libraries, use partial views, and integrate them into web applications for scalable and maintainable site development.
Everything related to a Razor Page can be compiled into a class library for easier reuse in multiple projects. With ASP.NET Core 3.0 and later, this can include static files such as HTML, CSS, JavaScript libraries, and media assets such as image files. A website can either use the Razor Page’s view as defined in the class library or override it.
Disabling compact folders for Visual Studio Code
Before implementing our Razor class library, we will learn about a Visual Studio Code feature, which is a compact folder feature. The compact folders feature means that nested folders, such as /Areas/MyFeature/Pages/ are shown in a compact form if the intermediate folders in the hierarchy do not contain files, as shown in the figure:
If we would like to disable the Visual Studio Code compact folders feature, complete the following steps:
On Windows, navigate to the “File | Preferences | Settings” option. On macOS, navigate to Code | Preferences | Settings.
In the Search settings box, enter compact.
Clear the Explorer: Compact Folders checkbox, as shown in the figure: