The type or namespace name 'IHttpContextAccessor' could not be found (are you missing a using directive or an assembly reference)


Question: How do you resolve an error that says "An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately."

This error happened when I added 

builder.Services.AddRazorPages().AddRazorRuntimeCompilation();


Login to See the Rest of the Answer

Answer: I understand that adding a .AddRazorRuntimeCompilation() allow you to utilize "dotnet watch run" with Razor MVC changes. Without this ".AddRazorRuntimeCompilation()" the "dotnet watch run" does not work with Razor changes.

In order to resolve this error, just add an HttpContextAccessor() to your dependency injection pipeline and the error should go away.

I hope this helps you. 





Kels said:

I got this error, any idea on how to solve it? An exception was thrown attempting to execute the error handler. Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: One or more compilation failures occurred:

Posted On: December 19, 2021 14:39:53 PM
Andrew said:

That is true, sometimes you will see this error is thrown as error: CS0246. Just add builder.Services.AddContextAccessor() in your program.cs file if you are using AspNet 6 and Visual Studio 2022.

Posted On: December 19, 2021 14:04:42 PM

For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]