Search code examples
c#asp.net-corerazor-pages

Visual Studio IDE Razor Implicit Transition is Not Working


I am upgrading from ASP.NET on .NET 4.7 to ASP.NET Core 8, and I am using the upgrade tool. I chose to upgrade incrementally in parallel using YARP as a reverse proxy. I now have the old dot-net framework project and a new dot-net core Project in the same solution.

The new framework does not support the old @helper in .cshtml razor files but does support the nearest equivalent as indicated here:

Implicit transition.

Unfortunately the design-time IDE thinks it is still looking at the old razor limitations because it shows squiggly error markers as shown in the following image.

Example of error indications.

The leading <span> tag is not recognized as an implicit transition from C# to html. You can also see in the screenshot that the explicit transition marker @:@{ is also not recognized.

As a test, I switched to an ASP.NET Core 3.1 web app I have and inserted a new function into a @functions block that used an implicit transition, and it did not complain.

There is something about the upgrade tool that is apparently not configuring the IDE to use the newer Razor features for ASP.NET Core 8.

Does anyone have a suggestion?


I believe I found the solution. I spotted an ignored warning about the Microsoft.AspNetCore.Razor.Design package with the message "Detected Razor language version downgrade".

I investigated and noted the parent package, Microsoft.AspNetCore.Mvc, was not the latest, as you can see below.

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />

I upgraded to 2.3.0 and the saw improvement in the error indicators in the code. I progressed further by testing what transition indicators worked and what didn't work, and so far (cross your fingers) it appears to be working.


My declaration of fixed was premature. Per a suggestion from @hady, I added package "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation", but since time had past since said suggestion, version 8 had progressed, so I picked version 8.0.12. After adding the package, I got the following warning.

Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file:

I did not perform the suggested conflict fix. Below is a snapshot of the NuGet Package Manager showing the installed packages. It shows 1 top level package (the new package) and several transitive packages. I have no idea what that means.

The original issue is still the issue as described, and I am looking for any suggestions.

I will repeat that the "@:" explicit transition marker is not recognized by razor, and does produce an error. This indicates to me that razor version is still down-version, even though no warning messages appears for that.

Installed Packages


Solution

  • What you've done is not enough. Your IDE still treating your .cshtml files as “old” Razor (from ASP.NET 4.x or Core 2.x) because of the outdated package references and a resulting Razor language‐version downgrade. 1-Upgrade your NuGet packages and remove the old Microsoft.AspNetCore.Razor.Design

    If you see this code in packages

    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" />
    

    Microsoft.AspNetCore.Razor.Design package was deprecated my friend

    you should Use the latest ASP.NET Core packages that match your target framework—for example, if you’re moving up to .NET 8:

      <TargetFramework>net8.0</TargetFramework>
    

    do this

    <ItemGroup>
        <!-- If you need MVC, or Razor runtime compilation, etc. -->
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0-preview.*" />
        ...
    </ItemGroup>
    

    It's in preview , so I suggest to use dotnet 7 or even 9

    2- Verify Your SDK and Razor Language Version

    In your .csproj, the top tag typically uses the Web or Razor SDK:

    <Project Sdk="Microsoft.NET.Sdk.Web">
        <PropertyGroup>
            <TargetFramework>net8.0</TargetFramework>
       </PropertyGroup>
    </Project>
    

    If you see <Project Sdk="Microsoft.NET.Sdk"> plus a 2.1 or something, that might force an old Razor language version.

    3. Clean & Rebuild

    the usual actions after any big change

    1-Close Visual Studio (or your IDE).

    2-Delete the bin and obj folders.

    3-Reopen and restore packages, then rebuild the project.

    4-Verify that no warnings about “Razor language version downgrade” remain.