How do I target .NET Standard 2.0 in a freshly created .NET Core 2.0 web app?

15,283

Solution 1

NET Standard is for class libraries. Applications must target netcoreapp* where * is a version number. The following shows the compatibility matrix: https://docs.microsoft.com/en-us/dotnet/standard/net-standard

For example, .NET Core 2 can consume .NET Standard version 2 and below.

Solution 2

It is not inherently possible to run a netstandard project as an executable. Since netstandard was designed to be used for libraries.

In order to develop your web application entirely in netstandard2.0, you would have to create a separate project that targets either .NET Core or .NET Framework to execute your library that contains your web app (developed using .NET Standard).

1. Executable Project (ex: console app)
   -- Target Framework: netcoreapp2.0 / net462

2. Web Application Project (library)
   -- Target Framework: netstandard2.0

You can use the following steps to change the target framework of your project.

Step 1. Target the desired framework

  1. Right-click on your project and select Edit *****.csproj

  2. In the .csproj file, you need to replace the target framework to the .NET Framework.

Example .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web"> //<-- note the .Web for the web template
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

For a list of the Target Framework Moniker (TFM) (ie, net47, netstandard2.0, netcoreapp2.0, etc.*) you can check this link out: https://docs.microsoft.com/en-us/dotnet/standard/frameworks

Step 2. Run dotnet restore

Go to your output window and run dotnet restore.

Note: Sometimes Visual Studio may misbehave (depending on which update you have installed), so you may have to close and re-open your Visual Studio. Otherwise, sometimes a clean/re-build may do the trick.


Targeting both frameworks

You can pick one or the other, or even target both frameworks.

<TargetFrameworks>netcoreapp2.0; net47</TargetFrameworks> //<-- note the plural form!
Share:
15,283

Related videos on Youtube

Geesh_SO
Author by

Geesh_SO

Updated on September 16, 2022

Comments

  • Geesh_SO
    Geesh_SO over 1 year

    I've just created a fresh project using dotnet new web. My Google-foo may be failing me, but I didn't find anything relating to my answer (please link to another SO answer, or relevant documentation if I've missed something obvious).

    If I want to ensure this new project is .NET Standard 2.0 compliant, what do I now do?

  • Tseng
    Tseng over 6 years
    netstandardx.y is for class libraries. They typically don't have an entry point and do not produce an executable file. The OP obviously wants to create an application (that's what dotnet new web does). If he were to create a class library he'd used dotnet new classlib instead
  • Svek
    Svek over 6 years
    @Tseng -- although you could technically create a basic console project and reference a netstandard library that has the entire code for the webhost...
  • Tseng
    Tseng over 6 years
    dotnet new web already creates an console application, that's why it targets netcorex.y in the first place. You can modularize the application (having controllers/Views split between multiple PCLs), but the main application still has to be netcorex.y or net4xy and this is where you also configure the Dependency Injection and where the Startup.cs resides. It makes little sense to make the whole application as PCL and then have a thin bootstrapping from this PCL, since the new csproj tooling allows to target multiple platforms from within the same project (unlike in the past)
  • aruno
    aruno over 5 years
    If in doubt create a new project with what you want to target, and compare the csproj file created with yours. These instructions worked great for me, but MS never seems to be able to stop changing things!