Search code examples
asp.net-corerazorvisual-studio-2022

Why am I getting Error CS0016 Could not write to output file in ASP.NET Core 7.0?


I'm getting this error:

Error CS0016 Could not write to output file 'C:\Users\jp2code\Documents\Visual Studio 2022\Projects\Core\Mvc1\Mvc1\obj\Debug\net7.0\generated\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Views_Shared__ValidationScriptsPartial_cshtml.g.cs' -- 'Could not find a part of the path 'C:\Users\jp2code\Documents\Visual Studio 2022\Projects\Core\Mvc1\Mvc1\obj\Debug\net7.0\generated\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Views_Shared__ValidationScriptsPartial_cshtml.g.cs'.' Mvc1 C:\Users\jp2code\Documents\Visual Studio 2022\Projects\Core\Mvc1\Mvc1\CSS

The file Visual Studio is looking for is here:

C:\Users\jp2code\Documents\Visual Studio 2022\Projects\Core\Mvc1\Mvc1\obj\Debug\net7.0\generated\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Views_Shared__ValidationScriptsPartial_cshtml.g.cs

It is not in that location, so Visual Studio is correct.

screenshot of missing file

My understanding is that a generated file like Views_Shared__ValidationScriptsPartial_cshtml.g.cs is supposed to come from a file in the path "Views > Shared > _ValidationScriptsPartial.cshtml", which DOES exist:

screenshot of existing file

How do I fix this error?


Solution

  • The error was from the file _ValidationScriptsPartial.cshtml in the project. The contents of this file was just 2 script files:

    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
    

    I excluded that one file from the project, and the project rebuilds fine.

    I don't understand that error. Why would Visual Studio add that file, and then the compiler complain about not being able to find it?