'the type or namespace name 'RuntimeCompilation' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.Razor'

If I write this line of code, services.AddMvc().AddRazorRuntimeCompilation(), in startup.cs and while publishing the project from Visual Studio 2019 on Azure, I encounter this error:

The type or namespace name 'RuntimeCompilation' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.Razor'

I've applied ASP.NET Core 3.1.

How can I solve this problem?



Solution 1:[1]

As it says in the docs

Install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.

Solution 2:[2]

Make sure that RuntimeCompilation package is really installed. In my case I was trying to install the latest version of the RuntimeCompilation and I didn't realize that the installation failed because my project configuration didn't support the latest package.

For some reason NuGet package (and visual studio for that matter) cannot understand what is the latest SUPPORTED version of the package based on the project configuration thus prompting always the latest version.

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 Anton Toshik
Solution 2 ilival