I tried multiple top answers until the below steps worked (ASP.NET project targeting .NET Framework 4.6.2, Visual Studio 2019 on a system with crazy restrictive group policies, March 2021).
I needed to:
run VS as Admin
in package manager console run
Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -v 2.0.1
Clean & Rebuild Solution
Without running VS as Admin, group policies blocked ps1 scripts that Update-Package needed to run.
PS. Before this worked, I tried numerous other answers (and ran git reset --hard after they failed). I do not know if any of them contributed to this eventually working. I tried: