Search code examples
asp.net-coreclass-library

How to create an ASP.NET Core class library


I want to use Visual Studio tooling around razor pages, components, views etc. for a project I am working on.

The scenario is the following: I want to have one main web application which is pluggable, so there shall be no direct reference to the plugins (which are class libraries).

  • class library should allow for razor pages etc
  • class library should allow for displaying controllers (that one already works)

What I did so far was changing the project file and changing the SDK to Microsoft.NET.Sdk.Web

You can reproduce this issue with this GitHub-link.

However the project I have changed now has "connected services" and launchSettings.json.

Is that a behavior I can ignore or will the project have side effects with the change I made?

enter image description here


Solution

  • What you're looking for (and what the switch to the SDK did) is a Razor Class Library, or RCL for short. It looks like you might have started with an ASP.NET Core site, which would explain the presence of launchSettings.json. An RCL is similar to an ASP.NET Core site in that you can include most of the same things: controllers, views, Razor pages, view components, tag helpers, static files, etc. However, notably, you will not have a Program.cs, Startup.cs, launchSettings.json, or any configuration files like appsettings.json. This is because, at the end of the day, it's just a library, and not something that runs on its own or stands on its own. You can make use of standard abstractions like IConfiguration/IOptions, ILogger, etc., but the actual implementation of these will come from your app, not the library. You should consult the documentation for more information.