Search code examples
c#.net-core.net-standard

MVC in a Core Library


I built this project and it contained a simple MVC type structure to help with debugging information. Now the company wants to use it in other applications like a Library The best way, I can describe it is

My Project Debugger Is a single controller with multiple end-points that render views (pretty basic), written in C#.

What I want is when another project wants to include my debugging, they would:

  1. Add Debugger as a reference using NuGet.
  2. Then in Start.cs the developer would use IApplicationBuilder.UseDebugger(IConfiguration, OtherInformation) or IServiceCollection.UseDebugger(IConfiguration, OtherInformation) and Debugger extension would do the rest like registering routes, building what it needs for dependency injection.

What I cannot figure out is what project type to use, I built it using .NET Core Web Application 3.1, but I do not think it is that. I also tried a .Net Standard class Library but I could not get a reference to controller to render views.

So to summarize, I want to convert part of my project to a library to allow for distribution to other applications.

A link to an example project would be helpful as well.


Solution

  • It sounds like you want a Razor Class Library.

    Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL). The RCL can be packaged and reused. Applications can include the RCL and override the views and pages it contains. When a view, partial view, or Razor Page is found in both the web app and the RCL, the Razor markup (.cshtml file) in the web app takes precedence.

    Edit: I just realised I answered a question about setting up a Razor Class Library last month, so that will give you a starting point, too.