Since updating to Visual Studio 2019 version 16.11.0 (today), compilation of Razor MVC views is failing on multiple cshtml
files in multiple projects:
error CS1576: The line number specified for #line directive is missing or invalid
I've tried to set fixed version of .NET Core SDK in global.json
file, which was placed in a root folder of MVC Web project, as described here, but that did not help as well.
I had the same error message with a .NET Core 5.0 web project in Visual Studio for Mac 8.10.11, after installing the Visual Studio for Mac 2022 preview with (as you guess) .NET Core 6.0 Preview. It might work on Windows too as mentioned in a now deleted answer.
The preview features as mentioned in @Failwyn's answer, under Preferences → Preview Features did not include the option to use previews of the .NET SDK. Adding global.json
as described in @Nenad's answer did not work either.
Fortunately, I did have another .NET Core 5.0 project which did compile, so I was able to figure out the cause. Or at least the solution: this was to remove the <LangVersion>
indications from the project file:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<LangVersion>latestmajor</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<LangVersion>latestmajor</LangVersion>
</PropertyGroup>
This surprises me, since it indicates the C# version, but apparently this influences the .NET SDK version as well.
UPDATE: Now that .Net 6 is no longer considered a preview, We have to change LangVersion from "Latest" to "9" in the csproj file to fix the issue.
This can be fixed by going to tools -> options -> Environment -> Preview Features -> Uncheck "Use previews of the .Net SDK (requires restart)".
Restart Visual Studio, and the projects will build again. I turned this on to test .Net 6 in Visual Studio 2019 and it broke all of my .Net 5 projects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With