Reusable UI Components for Multiple ASP.NET Core MVC Projects Using Razor Class Library

If you have multiple asp.net core mvc projects and want to share common UI between them, copying and pasting code across different projects is a huge pain and a massive debt that you'll pay; sooner rather than later. The Microsoft team has done a great job of making sure setting up an RCL (Razor Class Library) is equally a huge pain. Cheers to not having to go back to their docs again (at least for this).

ebeeraheem

View Profile
136 views
Jul 17, 2025
Updated Sep 06, 2025

Create a new Razor Class Library

dotnet new razorclasslib -o RazorUIClassLib

Replicate your project folder structure for the specific pages you want to reuse

So, if you want to reuse your login view for example, you'll typically place it in

Views/Account/Login.cshtml

Create a new _ViewImports.cshtml file at the root of your Views folder in the RCL and add

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

If you just want to share Views then that's it. You're done. Just reference the RCL in any project you wish to use it.

All your static assets are automatically exposed to the consuming project by default and you can access them like so

<link rel="stylesheet" href="~/_content/MyApp.SharedUI/css/site.css" />

The only new bit is _content/MyApp.SharedUI/