Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select framework version for .NET Core Class Library project - Visual Studio 2019

In Visual Studio 2019 if I try to create an ASP.NET Core web application project, I get to select the framework version in the following screen -

enter image description here

But when I try to create a .NET Core class library project I am prompted with the following screen which does not provide any option for selecting the framework version -

enter image description here

Clicking the Create button always creates the project right away taking the latest .NET Core version installed on my machine.

So, how can I select the framework version while creating the class library project? Or do I have to change it manually every time after creation ?

like image 880
atiyar Avatar asked Aug 02 '19 22:08

atiyar


People also ask

Can I use .NET framework library in .NET Core project?

NET Core, you need to use the . NET Framework. You can now reference . NET Framework libraries from .


1 Answers

The ASP.NET project creation dialog providing a framework selection seems to be an exception in .NET Core / Standard projects to me. At least since VS2019 with the new "New Project" dialog, you have the following options after creating the project with this dialog.

"Normally" (to my experience), you right-click the project file in the Solution Explorer, choose "Edit Project File" and modify the <TargetFramework> element by naming one of the valid target framework monikers. See MSDN about them.

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

You can also rename the element to TargetFrameworks (note the pluralized name) to build against multiple frameworks at the same time, which are ; separated:

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

  <PropertyGroup>
    <TargetFrameworks>net451;netstandard2.0;netcoreapp3.0</TargetFrameworks>
  </PropertyGroup>

</Project>

Alternatively, you can also choose "Properties" from the project right-click menu and select a framework via a slightly dated UI not supporting all of the new csproj features, like said multi targeting:

Project properties

If you need many new projects building against a specific framework, create a template csproj and just copy and rename it.

Also, if you want to build against preview versions of .NET Core in non-preview versions of VS, ensure you allow usage of them in Tools > Options > Environment > Preview Features.

like image 192
Ray Avatar answered Sep 19 '22 13:09

Ray