'Dotnet - How to change Target Framework using CLI?

How to change Dotnet target framework in .csproj using CLI?

I know how to do this using Visual Studio, but I want to do it using CLI.

Are there any commands like,
dotnet changeframework netcoreapp3.1?

Thanks.



Solution 1:[1]

Unfortunately, there isn't a built in command.

You can edit the .csproj file as a text file and modify the TargetFramework element directly.

You can override it at build time using the CLI by overriding the msbuild property: dotnet build -p:TargetFramwork=netcoreapp3.1.

You can use command line tools like sed (on Linux/macOS) to modify the file directly: sed -i -E 's|<TargetFramework>.*</TargetFramework>|<TargetFramework>netcoreapp3.1</TargetFramework>|' file.csproj. For Windows, try get-content.

Solution 2:[2]

I have net6.0 and net5.0 installed on my machine, and the default is set to net6.0. I wanted to target net5.0 framework for a webapi project and achieved it by typing this: dotnet new webapi -f net5.0 -n myApiProject

You can also see all the options on any template by typing dotnet new <template (example: webapi, classlib, etc.)> -h. This is where I found the -f|--framework option.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 omajid
Solution 2 Curious