How to setup an asp.net core 3.0 library project

As every software programmer I want to reuse my code in many projects.
This time a faced the problem to create a API controller on an external library and reference it on my asp.net core project.

There is not a particular template so I used the normal classlib by typing

dotnet new classlib

After that explore your csproj file and it should look like

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

At this point change the target framework with netcoreapp3.0 and add a new MyApiController.cs class like the following

using Microsoft.AspNetCore.Mvc;

namespace MyAspnetCoreLibrary
{
    [Route("api/[Controller]")]
    [ApiController]
    public class MyApiController : ControllerBase
    {

    }
}

Obviously a lot of asp.net core library are missing if you try to compile, so go back to your csproj and add a FrameworkReference tag

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" Version="3.0.0" />
  </ItemGroup>

</Project>

Now you should compile without errors

That’s all!

Loading...

Add Your Comment

* Indicates Required Field

Your email address will not be published.

*