I have followed this documentation for view components : https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-5.0#customize-the-view-search-path
But when i trying to customize the view search path it didn't work, i have used this configuration as the documentation mentioned :
services.AddMvc()
.AddRazorOptions(options =>
{
options.ViewLocationFormats.Add("/{0}.cshtml");
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
I also tried another configuration :
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationFormats.Add("/{0}" + RazorViewEngine.ViewExtension);
});
But nothing works
I just need to put view components in the root folder called Components, and i have areas in my application so each area will have its root folder called components
UPDATE
The problem appears only on areas, but on root working good as documentation mentioned
I have also tried to use this configuration but nothing works
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationFormats.Add("/{0}" + RazorViewEngine.ViewExtension);
options.ViewLocationFormats.Add("/Areas/Admin/{0}" + RazorViewEngine.ViewExtension);
});
UPDATE 2
after added new view location for area as mentioned before it didnt work on area and the searched location error not contains the new location of area i has added, but it just happen if you requested your View Component from area, but if you requested it from root you will find the searched location error contains the area location you have added.
If i requested view component from /Views/Home/Index.cshtml
Searched locations will be :
InvalidOperationException: The view 'Components/Test/Default' was not found. The following locations were searched:
/Views/Home/Components/Test/Default.cshtml
/Views/Shared/Components/Test/Default.cshtml
/Pages/Shared/Components/Test/Default.cshtml
/Components/Test/Default.cshtml
/Areas/Admin/Components/Test/Default.cshtml
So here the 2 locations i added works perfect !
If i requested view component from /Areas/Admin/Views/Home/Index.cshtml
Searched locations will be :
InvalidOperationException: The view 'Components/TestArea/Default' was not found. The following locations were searched:
/Areas/Admin/Views/Home/Components/TestArea/Default.cshtml
/Areas/Admin/Views/Shared/Components/TestArea/Default.cshtml
/Views/Shared/Components/TestArea/Default.cshtml
/Pages/Shared/Components/TestArea/Default.cshtml
So here the 2 locations i added are missed here !