I have an issue using a .Net 6 Class Library to create ViewComponent to use it many Web app.
I have create a Class library :
The code :
public class HelpComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
return View();
}
}
}
----- CsHtml
<h1>
Test
</h1>
----- Project config :
<ItemGroup>
<EmbeddedResource Include="Views/**/*.cshtml" />
</ItemGroup>
Afterthat I configure my Web app in asp.net 6 in program.cs :
builder.Services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(new EmbeddedFileProvider(
typeof(Peinture.Commons.ViewComponents.HelpComponent).Assembly, "commons"
));
});
And finally I try to import my componant inside a View :
@await Component.InvokeAsync(nameof(Peinture.Commons.ViewComponents.HelpComponent))
Do you have any idea why I got this error ? :
I've tried to change the code inside program.cs with different things I found on internet but nothing changed.
I reproduced your issue in my side.
Then I found document here and adding
AddRazorRuntimeCompilation()
.