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



如何在此处添加其他路径?

我想在一个名为“components”的项目文件夹中将其带有各自 Controller 的view组件命名为“components”。

/Components/{View Component Name}/{View Name}

我的动力:

我发现我的 View 组件具有自己的JS和CSS文件。我将所有JS打包并最小化到一个site.min.js中,并将所有CSS打包并最小化到它们的site.min.css中。 JS总是像$(function() { ... })一样,而CSS总是以顺序无关紧要的方式编写,因此在不知道顺序的情况下捆绑所有对象就不是问题。

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

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

最后,我想有一个像这样的“组件”(不是真正的“ 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/Components/Cart/Default.cshtml
  • Views/Home/Components/Cart/Default.cshtml
  • Areas/Blog/Views/Shared/Components/Cart/Default.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/

    10-12 07:26