How to make dotnet core select a lower version?

Viewed 11075

I have several versions of dotnet core in my system. But .Net Core project selects the latest version for building the project. Is there anything I can do to make sure that the project selects the correct version? I am using Visual Studio for development.

5 Answers

How to make dotnet core select a lower version?

From your description, I suppose you want to specify the framework to target when using the dotnet new command to create Asp.net Core application. If that is the case, you could use the -f|--framework <FRAMEWORK> property, for example: use the following command create a Asp.net Core 3.1 MVC application and named "aspnetcoreapp".

dotnet new mvc -f netcoreapp3.1 -o aspnetcoreapp  

[Note] The -f|--framework <FRAMEWORK> property available since .NET Core 3.0 SDK. More detail information about this property, see dotnet new command.

enter image description here

If you want to select the lower version, you could try to use Visual Studio 2019 to create an application, then after selecting the project template, you could select the .net core version like this:

enter image description here

To list all the versions of dotnet core installed in the system, open the command prompt and type the following command:

dotnet --info

If you want to know the specific version used for running the application, go to the folder location, and type cmd in the address bar at the top to open command prompt. Then type this in command prompt.

dotnet --version

If you want to change this version to some other version installed in the system, type the following in the command prompt.

dotnet new globaljson --sdk-version 2.2.100 --force

Here, I want to use 2.2.100. This will create a global.json file. This will tell Visual Studio which version of .NET Core to use.

This is the content of global.json file.

{
  "sdk": {
    "version": "2.2.100"
  }
}

By following this, you can switch between .NET Core versions. Otherwise, Visual Studio will only take the latest version installed.

Further Reading:

Create a new backend with sdk .NET 5

dotnet new webapi -n ProjectName -f net5.0

You can create global files in the root folders where you want specific versions. You can run the following in the command line in the selected folder:

dotnet new global

This creates a new file called "global.json". You can open this file and edit the version number. You can list all available SDK versions using dotnet --list-sdks

Here he complete documentation on how you can set versions. You may want to consider using "latestFeature" or "latestPatch" (detailed in the documentation). These help as the hardcoded .net core SDK versions might change over time.

Visual Studio will use the version of .NET Core that was selected while creating the project. If you have already created the project then you can set the version of .NET Core for the project from Visual Studio and on next build Visual Studio will use the configured version of .NET Core. See steps below to set the version of .NET Core if you have already created the project.

  1. Open project solution file in Visual Studio and Go to Solution Explorer
  2. In Solution Explorer, right click the project and click on Properties to open Project Properties Windows
  3. In Project Properties Window --> Application Tab --> Target Framework Dropdownlist --> Select the version of .NET Core that you want to use
  4. Build the solution.
Related