soundaranbu / Razor.Templating.Core

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

Home Page:https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

View Components Support

kmcguinn-caravan opened this issue · comments

First of all, just wanted to say that this library is awesome. Thank you for all the hard work. I'm pulling over some razor views from an existing solution that utilizes view components, and I'm having some problems getting the renderer to recognize them. I'm getting the follow error when I try to render a view that uses a view component. I've checked to be sure the ViewComponent suffix was there and have tried explicitly adding the ViewComponentAttribute to no avail. Note that these views do work in a normal .NET core web app. Thanks for the help!

System.InvalidOperationException: 'A view component named 'SomeViewComponent' could not be found. A view component must be a public non-abstract class, not contain any generic parameters, and either be decorated with 'ViewComponentAttribute' or have a class name ending with the 'ViewComponent' suffix. A view component must not be decorated with 'NonViewComponentAttribute'.'

Hi @kmcguinn-caravan, thanks for reporting the issue. I'll take a look at this. If possible, can you upload a sample project or add code snippets so that it's easier to identity & fix the issue?

Thanks @soundaranbu. I've created a simple project that illustrates the issue. There's a Razor Class Library with a single view and view component, a console app that uses the RazorTemplateEngine to render the view (throws error I referenced above), and a sample MVC web application that references the class library and renders the view and view component successfully. Let me know if you have any questions.

https://github.com/kmcguinn-caravan/RazorTemplatingViewComponents

Thanks @kmcguinn-caravan for taking your time to put this together. This helped me to fix the issue. I'll make a pre-release after doing some test.

Sounds good, thanks for jumping on this so quickly @soundaranbu !

@soundaranbu did you have an ETA when this fix would be available as a pre-release? I'm trying to decide whether to wait for the fix or implement a workaround for the time being. Thanks again for being so responsive.

Hi @kmcguinn-caravan , sorry for making you wait for the pre release. It's just that I'm not getting enough time on the weekdays due to office workloads.
Anyway I'll try to make a pre release today. Thanks!

New preview version v1.6.0-rc.1 has been released https://github.com/soundaranbu/RazorTemplating/releases/tag/v1.6.0-rc.1
Kindly try it out & let me know if it works or breaks

Thanks!

No worries @soundaranbu, thanks again for the quick turnaround. Unfortunately, now it looks like the views from the Razor Class Library are not being found. This is the error I get in the sample project:

Unable to find view '~/Views/Home/Index.cshtml'. The following locations were searched: ~/Views/Home/Index.cshtml

oops.. I'll look into this

I've pushed a new version v1.6.0-rc.2 @kmcguinn-caravan
Kindly remove the these lines in your repository

and update the version in this line to

    <PackageReference Include="Razor.Templating.Core" Version="1.6.0-rc.2" />

Hey @soundaranbu, that looks to have gotten past the previous errors, but now I'm seeing the following error when passing a model to the view component. I've updated the sample project to recreate the issue.

The model item passed into the ViewDataDictionary is of type 'RazorTemplatingViewComponent.Views.Shared.Components.SomeComponent.SomeModel', but this ViewDataDictionary instance requires a model item of type 'RazorTemplatingViewComponent.Views.Shared.Components.SomeComponent.SomeModel'.

Ok I'll look into it

For some reason, the same code works in net5.0
Not sure why this occurs in netcoreapp3.1
I'll need some time to debug the difference. For the time being, if possible you can upgrade to net5.0 to get this feature working. @kmcguinn-caravan

@soundaranbu I can confirm it does appear to be working when I upgrade to .net5.0. Since this feature is running in it's own project, this should be fine. Thank you again for all the help!

Glad to know. Thanks!