Something non-obvious (or obviously not found via Google-foo) changed in ASP.NET Core 2.0 with regard to how it's finding views. All of the views associated with the generic area route ("{area:exists}/{controller=Home}/{action=Index}/{id?}"
) will execute their actions and find their associated view as expected, but if I specify a less generic route, like "Forums/Recent/{page?}"
, it won't find the view. I can't emphasize enough, the code in the controller action fires so it's correctly using the route. It's just not looking for the view in the right place. It's only looking in shared places:
InvalidOperationException: The view 'Recent' was not found. The following locations were searched:
/Areas/Forums/Views/Shared/Recent.cshtml
/Views/Shared/Recent.cshtml
It's not looking in /Areas/Forums/Views/Forum/Recent.cshtml, which by convention matches the controller.
My controllers are not in the same project as the views, if that matters. Again, the controller action executes, but it doesn't even look in the right place for the view. It worked correctly in v1.1.
In a View, you can invoke a view component one of two ways: use a View's Component property or add a Tag Helper to the View. For my money, the simplest method is to simply call the InvokeAsync method from the View's Component property, passing the name of your view component and its parameters.
View Components that inherit from ViewComponent class are able to call the View() method. When no specific view is called out, the view is searched in the following two ways and in the following order: Views/{ControllerName}/Components/{ComponentName}/Default. cshtml.
ViewComponents are generated from a C# class derived from a base class ViewComponent and are typically associated with a Razor files to generate markup.
Turns out this is a bug in the view engine that comes up when you have "page" in your route definition. The bits that look for the view don't look in the right place:
https://github.com/aspnet/Mvc/issues/6660
The team has moved the bug into the 2.0.1 release:
https://github.com/aspnet/Mvc/milestone/38
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With