Asp.net core 组件位于不同区域,更改默认搜索位置
在ASP.NET核心MVC的新特性中,我对这些组件很感兴趣。我的问题是我想把组件放在一个特定的区域,但我不能更改组件视图的默认搜索位置 这是我的区域示例。我将网站的标题组件放在不同的区域:Asp.net core 组件位于不同区域,更改默认搜索位置,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,在ASP.NET核心MVC的新特性中,我对这些组件很感兴趣。我的问题是我想把组件放在一个特定的区域,但我不能更改组件视图的默认搜索位置 这是我的区域示例。我将网站的标题组件放在不同的区域: Areas Site Components HeaderViewComponent Views Shared Components Header Default.cshtml 这是组件的默认搜索位置。现在,在我的标题组件中
Areas
Site
Components
HeaderViewComponent
Views
Shared
Components
Header
Default.cshtml
这是组件的默认搜索位置。现在,在我的标题组件中,我发送带有模型的视图:
public async Task<IViewComponentResult> InvokeAsync()
{
return View(headerModel);
}
这不管用。我有一个错误:
InvalidOperationException: The view 'Components/Header/Default' was not found. The following locations were searched:
/Views/Home/Components/Header/Default.cshtml
/Views/Shared/Components/Header/Default.cshtml
这个错误是因为我调用了区域外的组件。我有另一个组件,我称之为它工作的区域。我试图定义一个特定的搜索位置
app.UseMvc(routes =>
{
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
services.AddMvc()
.AddRazorOptions(options =>
//"~/Areas/Site/Views/Shared/Components/Header/Default.cshtml"
options.ViewLocationFormats.Add("/Areas/{3}/Views/Shared/Components/{1}/{0}.cshtml");
});
这不适用于组件。组件似乎只有它们的默认值。我不知道如何改变它
我通过发送视图的全名找到了解决方案,这很有效:
public async Task<IViewComponentResult> InvokeAsync()
{
return View("~/Areas/Site/Views/Shared/Components/Header/Default.cshtml", headerModel);
}
public异步任务InvokeAsync()
{
返回视图(“~/Areas/Site/Views/Shared/Components/Header/Default.cshtml”,headerModel);
}
但是我不喜欢它。如果有人知道如何覆盖组件默认搜索位置,这会有所帮助:)请不要使用名称MVC6,因为没有MVC6。仅限ASP.NET MVC 5.x(高达5.2)和ASP.NET Core MVC 1.x版本,谢谢您的更正。你知道如何解决这个问题吗?请不要使用MVC6这个名字,因为没有MVC6。仅限ASP.NET MVC 5.x(高达5.2)和ASP.NET Core MVC 1.x版本,谢谢您的更正。你知道怎么解决这个问题吗?
public async Task<IViewComponentResult> InvokeAsync()
{
return View("~/Areas/Site/Views/Shared/Components/Header/Default.cshtml", headerModel);
}