Is there a way to check if a ViewComponent's View actually exists before trying to render it?
I know there are FindView and GetView methods for searching Views from e.g. Controller, I'm looking for something similar for ViewComponents.
Inside a ViewComponent, there's a ViewEngine, but when I try using that like ...
ViewEngine.FindView(ViewContext, "SomeView", false);
... the SearchedLocations for the result is not looking for ViewComponents:
[0]: "/Views/Home/SomeView.en-US.cshtml"
[1]: "/Views/Home/SomeView.en.cshtml"
[2]: "/Views/Home/SomeView.cshtml"
[3]: "/Views/Shared/SomeView.en-US.cshtml"
[4]: "/Views/Shared/SomeView.en.cshtml"
[5]: "/Views/Shared/SomeView.cshtml"
[6]: "/Pages/Shared/SomeView.en-US.cshtml"
[7]: "/Pages/Shared/SomeView.en.cshtml"
[8]: "/Pages/Shared/SomeView.cshtml"
Note: Asking this question because the try-catch block does not catch the exception that occurs with a missing view, for example ...
public IViewComponentResult Invoke()
{
try
{
return View();
}
catch (Exception e)
{
return Content(string.Empty);
}
}
... would cause an unhandled exception due to missing view file.