aspnetcore: View component not being rendered using tag helper syntax

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am trying to use a view component using the tag helper syntax <vc:[view-component-name]> and since the latest .NET release (7.0.200) it is not being rendered if I run my published application in Linux, instead I am seeing the <vc:[view-component-name]> markup in the HTML being sent to the client.

Expected Behavior

I expect the tag helper to work as expected and invoke the view component, not render the raw markup from the cshtml-file.

Steps To Reproduce

  1. Create ASP.NET project using .NET 7.0.200 on Linux
  2. Add a view component
  3. Render it using the tag helper syntax
  4. Publish the project dotnet publish
  5. Run the project ./Project

Exceptions (if any)

I am not getting any error messages or exceptions.

.NET Version

7.0.200

Anything else?

Things I have tried:

  • It works fine when I use dotnet run on both Linux and Windows
  • It works fine when I publish on Windows using dotnet publish
  • It does not work in Linux when using dotnet publish
  • await Component.InvokeAsync works as expected
  • It was working fine with the last .NET release before 7.0.200

Read the comments, other users have reported the same problem in many more cases.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 20
  • Comments: 32 (1 by maintainers)

Most upvoted comments

Rendering of view components using tag helpers is broken with the release of the new SDKs of 2023-02-14. I believe this is the versions where this is broken:

  • 7.0.200
  • 7.0.103
  • 6.0.406
  • 6.0.309
  • 6.0.114

It is not related to Linux. Not even specifically to Visual Studio.

We see the razor syntax in our HTML like this: <vc:hero-image></vc:hero-image> Noticed this last week already and (like already suggested) pinned to a lower SDK version using global.json

(@DavidZidar Thanks for changing the title, was just going to suggest that)

(edited to add SDK versions)

If I’m reading this correctly, this has been broken since 02/14? I’m not clear if this is a Visual Studio issue or an SDK issue.

I’m using .Net 7.0.3. I published nearly two weeks code changes to my staging environment for the next phase of testing and user acceptance and now my application is nearly non-functional.

Are there any workarounds for those of us on .Net 7? Or is it VS-related?

This is definitely an SDK issue with 7.0.200. It seems that the latest SDK available on your machine will be used by default. Meaning that even if your project is targeting .NET 6, the v7 SDK will be used if available.

You can use a global.json file to pin the build to a specific SDK version. Add the file next to your solution file with these contents:

{
  "sdk": {
    "version": "7.0.103"
  }
}

We use Azure DevOps to perform our builds and use the UseDotNet task to specify which version of the SDK to use for the builds (this needs to happen before any building/restoring/etc.):

- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '7.0.103'
    includePreviewVersions: true

Alternatively, you can convert all the ViewComponent tag helpers in your project(s) to use the @await Component.InvokeAsync(...) method instead.

I have the same issue but I’m not publishing, I am just running in development using dotnet run

Earlier tonight I upgraded Visual Studio to 17.5 (from 17.4 if memory is correct…) Prior to the update the view component tag-helper syntax worked as expected. Afterwards, only seems to work if I use Component.InvokeAsync() syntax.

I’m only 1 developer working on my local development machine (Windows 11).

Following SDKS now installed after VS upgrade:

dotnet --list-sdks
6.0.406 [C:\Program Files\dotnet\sdk]
7.0.200 [C:\Program Files\dotnet\sdk]

Here’s a quick rundown of the setup for this simple VC:

PROJECT TARGET SDK: <TargetFramework>net7.0</TargetFramework>

<PROJECTDIR>/Views/_ViewImports.cshtml: (some usings removed for brevity)

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, FDMWeb   // FDMWeb is the project namespace

<PROJECTDIR>/ViewComponents/WorkspaceSelector.cs:

public class WorkspaceSelector : ViewComponent {
....
    public async Task<IViewComponentResult> InvokeAsync(int userId, string requestedRoles, string returnUrl)
    {
        .....
        return View(vm);
    }
}

<PROJECTDIR>/Views/Shared/Components/WorkspaceSelector/Default.cshtml: …contains the view code for the component

<PROJECTDIR>/Views/Personnel/Index.cshtml (the view where I use the view component):

<div>
   // NOTE: I ADDED THIS AS A TEST, AND IT WORKS AS EXPECTED
   @await Component.InvokeAsync("WorkspaceSelector", new { userId = Model.UserId, requestedRoles = "Administrator,User", returnUrl = "/Personnel/Index" })

   // NOTE: THIS NOW RENDERS AS-IS 
   <vc:workspace-selector user-id="@Model.UserId" requested-roles="Administrator,User" return-url="/Personnel/Index">
        </vc:workspace-selector>
</div>

Hope this was helpful. If not, just delete/cancel me. 😃

Ok, good, that does sound like either the same or a related issue. In my case it only breaks when I publish and then run the published application and only in Linux.

Make sure you add the correct usings.

They are correct, the app works as intended (renders vc tag helpers) in a Windows Sandbox environment with an older .NET SDK

Anyone know if Microsoft will have this fixed soon?

Probably not until the release in March (2-3 weeks from now). They released a small update for the SDK a few days ago (7.0.201) without fixing this and I doubt that they will do another extra release before the next regular one.

Adding the global.json worked for me. This threw me for a loop because I thought the upgrade to the newest version of Visual Studio caused it. But then downgrading did not help.

I’m not clear if this is a Visual Studio issue or an SDK issue.

This should be a SDK issue as I am using SDK to build and publish not Visual Studio

They are aware of the issue and are working on getting a fix out. https://github.com/dotnet/razor/issues/8281#issuecomment-1446996618

Same happened for us too (our existing project VC rendering stopped working)

Looking further into this, it appears that if you pin yourself to 6.0.404 you are ok, but if compiling against 6.0.406 it fails as outlined as well

I have detailed logs from a build & unit test that fails in this transition

I’m facing same issue with SDK 7.0.200. Also same as https://github.com/dotnet/razor/issues/8281 It’s horrible issue because no error and warning at build.

I have rewrote <vc: to @await Component.InvokeAsync as workaround.

Can confirm the same issue started after updating Visual Studio 17.5, even without updating packages.

I have the same problem. But I’m publishing to a Windows Web App in Azure. However, I am using a linux build agent in Azure DevOps. There is a StackOverflow question for this as well.

I have the same problem when publishing the application in a Linux Web App in Azure.

I am publishing the application using Azure DevOps using a linux vm.

The View Components are not rendered using a Tag Helper … Only using Component.InvokeAsync.

In my computer, a MAC, it works both. using a Tag Helper or Component.InvokeAsync