My app implements plugin system (razor libraries). App is using razor runtime compilation. Razor library contains few ViewComponents with custom structure described below.
Razor library structure:
- Components/SampleComponent.cs
- Views/SampleComponent.cshtml
SampleComponent implementation:
public class SampleComponent: ViewComponent
{
public SampleComponent()
{
}
public IViewComponentResult Invoke()
{
// return View($"~/Views/{nameof(SampleComponent)}.cshtml");
return View($"~/Plugins/MyPlugin1/Views/{nameof(SampleComponent)}.cshtml");
}
}
Plugins are in this structure in asp.net core app:
- Plugins/{plugin_name}
- Plugins/{plugin_name}/Views/SampleComponent.cshtml
Problem is if I try to render commented line app is not crashing on render, however it returns empty content. If I render with absolute path then app crashes cause it cant find View cshtml.
I would like to avoid extending IViewLocationExpander.
What can cause my empty content or cant find view problem?
Expected render path:
public IViewComponentResult Invoke()
{
return View($"~/Plugins/MyPlugin1/Views/{nameof(SampleComponent)}.cshtml");
}
Exception:
InvalidOperationException: The view ‘Components/SampleComponent/~/Plugins/MyPlugin1/Views/SampleComponent.cshtml’ was not found. The following locations were searched: ~/Plugins/MyPlugin1/Views/SampleComponent.cshtml
luve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.