netcore5 View components Customize the view search path not working on areas

Viewed 971

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 !

1 Answers

There are 2 different configurations of view location formats for the root & area scopes. Which one being used depends on where the code runs (at the time the view will be searched). So if it's inside the root scope, we have RazorViewEngineOptions.ViewLocationFormats but if it's inside area scopes, we have RazorViewEngineOptions.AreaViewLocationFormats.

So in your case, you need to add this:

options.AreaViewLocationFormats.Add("/Areas/Admin/{0}" + RazorViewEngine.ViewExtension);

For a general format that can apply all areas, we can use the placeholder {2} which is designed for area name, like this:

options.AreaViewLocationFormats.Add("/Areas/{2}/{0}" + RazorViewEngine.ViewExtension);
Related