Why View Component is not rendered in production?
Asked Answered
C

1

13

On a Net Core 7 project I am rendering a View Component on a Razor page:

<vc:footer></vc:footer>

The component C# code is:

public class FooterViewComponent : ViewComponent {
  public FooterViewComponent() { }

  public IViewComponentResult Invoke() {
    return View("Footer");
  }
} 

And the HTML, in file /Pages/Shared/Components/Footer/Footer.cshtml is:

<footer>My Footer Test</footer>

When I run the application in my computer the footer is rendered.

But when I publish to the server, in production, the footer view component is not rendered.

When inspect the code of the page in the browser and it shows:

<vc:footer></vc:footer>

Any idea why? I really have no idea why this happens.

Crunch answered 20/2, 2023 at 17:54 Comment(8)
Which server do you publish? When you use @await Component.InvokeAsync(xxxx) to render view component, Can it be rendered successfully after published?Propene
Yes, if I use InvokeAsync the view component is rendered in production machine. Why is that? What should I change to use the tag helper? BTW, I am hosting the application in a Linux Web App in Microsoft Azure.Crunch
I just ran into the same problem after updating my app from .NET 6 to 7. I'm hosting in a Windows Web App in Azure. using Component.InvokeAsync does resolve the issue for me as well.Rivet
There's an issue in GitHub for this: github.com/dotnet/aspnetcore/issues/46740Rivet
Related question #75539399Uptodate
Same problem here, and i have hundreds view components in my apps, locally everything is fine but when i deploy them to linux they are not renderedCountersign
Not working on Windows either. Just wasted a couple hours wondering what the hell I did wrong.Brooks
As manies from the Github link above, I started to experience this problem after VS update to 17.5. This is confirmed bug related to SDK which is installed along with VS update. MS team promised to fix it in 17.5.2Runlet
G
1

This is a known bug and can be tracked with this GitHub issue.

When I run the application in my computer the footer is rendered.

But when I publish to the server, in production, the footer view component is not rendered.

From what I understand this is an issue on Linux servers, so if you are developing on a Windows machine this could be the difference that you are experiencing. I am developing on a Mac and it breaks locally as well as on the Linux server I deploy to.

Workaround

The current workaround is to use global.json to pin to an earlier working version. I was using SDK version 7.0.201 and it was breaking. However using the previous version 7.0.104 fixes the issue for me locally (Mac) and in production (Linux).

global.json

{
  "sdk": {
    "version": "7.0.104"
  }
}
Grayish answered 20/3, 2023 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.