Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to render a Razor View Component? #230

Open
betfix opened this issue Feb 21, 2022 · 2 comments
Open

How to render a Razor View Component? #230

betfix opened this issue Feb 21, 2022 · 2 comments
Labels
General Enhancement New feature or request

Comments

@betfix
Copy link

betfix commented Feb 21, 2022

I created a console app with Statiq.Web, the CleanBlog theme and a few pages. Then I added a class named "CategoriesViewComponent" at project root and added this line to _layout.cshtml:

@await Component.InvokeAsync("Categories")
//or @await Component.InvokeAsync(typeof(MyProject.CategoriesViewComponent))

This causes the app to throw error:

[ERRO] Content/PostProcess » RenderContentPostProcessTemplates » ExecuteIf » ExecuteIf » RenderRazor » [D:/MyProjects/MyProject/MyProject/input/home.cshtml => home.html] A view component named 'Categories' 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'.

The view component class is there and fulfills the conditions for being a view component. Is it possible to make it visible to Razor engine when running from Statiq?

@daveaglick
Copy link
Member

In theory I think component support should be possible. Statiq uses the ASPNET Core 3.x Razor engine, so anything that works there should be capable of working here. The challenge is that just having the engine isn't enough - it takes a fair bit of reflection and hacking to light up various features.

So far I've focused on common functionality like layouts, partials, and tag helpers. It's likely component support is possible with a little more hacking, but it's not a feature I've tested or directly supported so far.

I'm dedicating most of my brain cells to getting Statiq Docs out the door right now. I'll try to take a look at Razor components at some point this week - if it's easy enough to light up, I'll add it to the next release. If not, support for components may need to wait on the backlog for a while until I (or someone else) can get to it and figure out what's not working.

@daveaglick daveaglick transferred this issue from statiqdev/Statiq.Web Feb 21, 2022
@daveaglick
Copy link
Member

Transferring this issue to Statiq Framework since that’s where Razor support lives.

@daveaglick daveaglick added the General Enhancement New feature or request label Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
General Enhancement New feature or request
Development

No branches or pull requests

2 participants