gpt4 book ai didi

c# - ASP.NET Core MVC View 组件搜索路径

转载 作者:行者123 更新时间:2023-12-01 16:15:47 24 4
gpt4 key购买 nike

在此处的文档中: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.2

The runtime searches for the view in the following paths:

/Views/{Controller Name}/Components/{View Component Name}/{View Name}
/Views/Shared/Components/{View Component Name}/{View Name}
/Pages/Shared/Components/{View Component Name}/{View Name}

如何在此处添加另一条路径?

我希望将我的 View 组件及其各自的 Controller 放在一个名为组件的项目文件夹中,如下所示。

/Components/{View Component Name}/{View Name}
<小时/>

我的动机:

我发现我的 View 组件有自己的 JS 和 CSS 文件。我将所有 JS 捆绑并最小化在一个 site.min.js 中,并将所有 CSS 捆绑并最小化在其 site.min.css 中。 JS 总是类似 $(function() { ... }) ,而 CSS 总是以顺序无关紧要的方式编写,因此在不知道顺序的情况下捆绑所有内容不是问题。

其中一些 View 组件具有可以更改其在服务器上的状态的 JavaScript,例如对 Controller 操作的 AJAX 调用返回一些 JSON 或整个 View 组件的 HTML。

由于 Controller 只是一个 C# 类,它们可以位于任何文件夹中,但将具有相关 AJAX 操作的 Controller 移动到“Views”文件夹中感觉很愚蠢。

最后我想要一个像这样的“组件”(不仅仅是“ View 组件”):

/Components/SomeViewComponent/Default.cshtml
/Components/SomeViewComponent/SomeViewComponentController.cs
/Components/SomeViewComponent/SomeViewComponent.cs
/Components/SomeViewComponent/SomeViewComponent.css
/Components/SomeViewComponent/SomeViewComponent.js
/Components/SomeViewComponent/SomeViewComponent.en.resx
/Components/SomeViewComponent/SomeViewComponent.cs-CZ.resx

最佳答案

因此,在深入研究 aspnetcore 存储库一个小时后,我发现该组件的搜索路径是 hardcoded然后与普通 View 搜索路径结合。

// {0} is the component name, {1} is the view name.
private const string ViewPathFormat = "Components/{0}/{1}";

然后将该路径发送到 View 引擎

result = viewEngine.FindView(viewContext, qualifiedViewName, isMainPage: false);

View 引擎然后使用可配置的 View 路径生成完整路径。

  • Views/Shared/<strong>Components/Cart/Default</strong>.cshtml
  • Views/Home/<strong>Components/Cart/Default</strong>.cshtml
  • Areas/Blog/Views/Shared/<strong>Components/Cart/Default</strong>.cshtml

如果您想将 View 组件放入我想要的名为“Components”的根文件夹中,您可以执行类似的操作。

services.Configure<RazorViewEngineOptions>(o =>
{
// {2} is area, {1} is controller,{0} is the action
// the component's path "Components/{ViewComponentName}/{ViewComponentViewName}" is in the action {0}
o.ViewLocationFormats.Add("/{0}" + RazorViewEngine.ViewExtension);
});

在我看来,这有点丑陋。但它确实有效。

您也可以像这样编写自己的扩展器。

namespace TestMvc
{
using Microsoft.AspNetCore.Mvc.Razor;
using System.Collections.Generic;

public class ComponentViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
// this also feels ugly
// I could not find another way to detect
// whether the view name is related to a component
// but it's somewhat better than adding the path globally
if (context.ViewName.StartsWith("Components"))
return new string[] { "/{0}" + RazorViewEngine.ViewExtension };

return viewLocations;
}

public void PopulateValues(ViewLocationExpanderContext context) {}
}
}

在 Startup.cs 中

services.Configure<RazorViewEngineOptions>(o =>
{
o.ViewLocationExpanders.Add(new ComponentViewLocationExpander());
});

关于c# - ASP.NET Core MVC View 组件搜索路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com