Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 2.0 only looking for view in Shared folders

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.

like image 920
Jeff Putz Avatar asked Aug 17 '17 00:08

Jeff Putz


People also ask

How can I invoke a view component in asp net core?

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.

What is the default path of view for ViewComponent?

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.

Which is the common object is used to return view component and some data?

ViewComponents are generated from a C# class derived from a base class ViewComponent and are typically associated with a Razor files to generate markup.


1 Answers

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

like image 153
Jeff Putz Avatar answered Oct 14 '22 06:10

Jeff Putz