在此处的文档中:
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.2
运行时在以下路径中搜索视图:
/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}
如何在此处添加其他路径?
我想在一个名为project的项目文件夹中将具有其各自控制器的view组件命名为component。
/Components/{View Component Name}/{View Name}
我的动力:
我发现我的视图组件具有自己的JS和CSS文件。我将所有JS捆绑和最小化在一个site.min.js
中,并将所有CSS捆绑和最小化在它们的site.min.css
中。 JS总是像$(function() { ... })
之类的东西,而CSS总是以顺序无关紧要的方式编写,因此在不知道顺序的情况下捆绑所有对象就不是问题。
这些视图组件中的某些组件具有可更改其在服务器上的状态的javascript,例如AJAX调用控制器的操作,该操作返回一些JSON或整个视图组件的HTML。
由于控制器只是C#类,因此它们可以位于任何文件夹中,但是将具有相关AJAX操作的控制器移动到“视图”文件夹中感觉很愚蠢。
最后,我想有一个像这样的“组件”(不是真正的“视图组件”):
/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,然后与常规视图搜索路径结合在一起。
// {0} is the component name, {1} is the view name.
private const string ViewPathFormat = "Components/{0}/{1}";
然后将此路径发送到视图引擎
result = viewEngine.FindView(viewContext, qualifiedViewName, isMainPage: false);
然后,视图引擎使用可配置的视图路径生成完整路径。
Views/Shared/Components/Cart/Default.cshtml
Views/Home/Components/Cart/Default.cshtml
Areas/Blog/Views/Shared/Components/Cart/Default.cshtml
如果要根据需要将视图组件放置在名为“ 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());
});
ASP.NET Core Singleton实例与瞬态实例的性能 - c#在ASP.NET Core依赖注入中,我只是想知道注册Singleton实例是否会比注册Transient实例更好地提高性能?在我看来,对于Singleton实例,创建新对象和相关对象只需花费一次时间。对于Transient实例,此成本将针对每个服务请求重复。因此Singleton似乎更好。但是,在Singleton上使用Transient时,我们可以获得多…
ASP.NET Core-在Singleton注入上存储库依赖项注入失败 - c#我正在使用SoapCore为我的ASP.NET Core MVC应用程序创建Web服务。我正在使用Entity Framework Core和简单的存储库模式来获取我的数据库数据。我通过Startup.cs中的.AddSingleton()注入存储库类:services.AddSingleton<IImportRepository, ImportRep…
如何在ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制 - c#我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…
ASP.NET MVC Core 3.0 API将枚举序列化为字符串 - c#如何在ASP.NET MVC Core 3.0中将Enum字段序列化为String而不是Int?我不能用旧的方式做。services.AddMvc().AddJsonOptions(opts => { opts.JsonSerializerOptions.Converters.Add(new StringEnumConverter()); }) 我收到…
Asp.net Core 2.1记录 - c#我想了解登录asp.net cortex 2.1的问题。我的目标是注册所有可能导致kestrel服务器损坏的严重错误。据我从文档中了解Microsoft.Extensions.Logging和Serilog,您需要在每个控制器中创建一个记录器,并设置记录条件(例如try / catch)。是否有一种更方便的方法可以从一个地方记录项目中的任何严重错误?我将不胜…