netcore5视图组件自定义视图搜索路径不适用于区域 [英] netcore5 View components Customize the view search path not working on areas

查看:84
本文介绍了netcore5视图组件自定义视图搜索路径不适用于区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照以下文档查看视图组件:

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);
});

但是没有任何作用

我只需要将视图组件放在名为Components的根文件夹中,并且我的应用程序中就有区域,因此每个区域都将有其名为root的根文件夹

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);
});

更新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.

如果我从/Views/Home/Index.cshtml

搜索到的位置将是:

InvalidOperationException:找不到视图组件/测试/默认".搜索了以下位置:

InvalidOperationException: The view 'Components/Test/Default' was not found. The following locations were searched:

/Views/Home/Components/Test/Default.cshtml

/Views/Home/Components/Test/Default.cshtml

/Views/Shared/Components/Test/Default.cshtml

/Views/Shared/Components/Test/Default.cshtml

/Pages/Shared/Components/Test/Default.cshtml

/Pages/Shared/Components/Test/Default.cshtml

/Components/Test/Default.cshtml

/Components/Test/Default.cshtml

/Areas/Admin/Components/Test/Default.cshtml

/Areas/Admin/Components/Test/Default.cshtml

所以在这里我添加的两个位置非常完美!

So here the 2 locations i added works perfect !

如果我从/Areas/Admin/Views/Home/Index.cshtml请求视图组件

搜索到的位置将是:

InvalidOperationException:找不到视图组件/TestArea/默认".搜索了以下位置:

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/Home/Components/TestArea/Default.cshtml

/Areas/Admin/Views/Shared/Components/TestArea/Default.cshtml

/Areas/Admin/Views/Shared/Components/TestArea/Default.cshtml

/Views/Shared/Components/TestArea/Default.cshtml

/Views/Shared/Components/TestArea/Default.cshtml

/Pages/Shared/Components/TestArea/Default.cshtml

/Pages/Shared/Components/TestArea/Default.cshtml

所以这里我添加的2个位置在这里错过了!

So here the 2 locations i added are missed here !

推荐答案

对于根&区域范围.使用哪种代码取决于代码在哪里运行(在搜索视图时).因此,如果它在根范围内,则具有 RazorViewEngineOptions.ViewLocationFormats ;但是,如果它在区域范围内,则具有 RazorViewEngineOptions.AreaViewLocationFormats .

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);

对于可以应用于所有区域的通用格式,我们可以使用专为区域名称设计的占位符 {2} ,如下所示:

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);

这篇关于netcore5视图组件自定义视图搜索路径不适用于区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆