gpt4 book ai didi

asp.net-core-mvc - Asp.Net Core MVC 中的 IViewLocationExpander.PopulateValues() 是什么

转载 作者:行者123 更新时间:2023-12-04 08:35:50 26 4
gpt4 key购买 nike

我正在使用 ASP.NET MVC CORE。我已经实现了自己的 ViewLocationExpander,这样我就可以按照我想要的方式构建我的项目,并将我的 View 放置在我喜欢的地方。

这是通过实现一个继承自 IViewLocationExpander 的类来实现的。并且大部分工作都通过以下方法进行:

ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)

一切都很好,但接口(interface)定义了我不知道如何正确实现的第二种方法:
PopulateValues(ViewLocationExpanderContext context)

我已经在互联网上阅读了有关此接口(interface)的文章,但没有人真正提供有关此方法的确切用途的太多信息,只是模糊地说明了它如何帮助缓存。

如果有人能解释框架如何使用此方法以及我如何适本地使用它来帮助缓存(如果这确实是它的用途),我将不胜感激。

最佳答案

还没有弄乱它足以给你一个具体的答案,但看看 IViewLocationExpander.PopulateValues(ViewLocationExpanderContext context) 在 ASP.NET MVC GitHub 存储库上:

public interface IViewLocationExpander
{
/// <summary>
/// Invoked by a <see cref="RazorViewEngine"/> to determine the values that would be consumed by this instance
/// of <see cref="IViewLocationExpander"/>. The calculated values are used to determine if the view location
/// has changed since the last time it was located.
/// </summary>
/// <param name="context">The <see cref="ViewLocationExpanderContext"/> for the current view location
/// expansion operation.</param>
void PopulateValues(ViewLocationExpanderContext context);

// ...other method declarations omitted for brevity
}
可读性格式:

"Invoked by a RazorViewEngine to determine the values that would be consumed by this instance of IViewLocationExpander. The calculated values are used to determine if the view location has changed since the last time it was located.

Parameters:

context: The ViewLocationExpanderContext for the current view location expansion operation."


我已经查看了一些实现此接口(interface)的类——一些声明了该方法但将其留空,另一些则实现了它。
NonMainPageViewLocationExpander.cs :
public void PopulateValues(ViewLocationExpanderContext context)
{
}
LanguageViewLocationExpander.cs :
private const string ValueKey = "language";

public void PopulateValues(ViewLocationExpanderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

// Using CurrentUICulture so it loads the locale specific resources for the views.
#if NET451
context.Values[ValueKey] = Thread.CurrentThread.CurrentUICulture.Name;
#else
context.Values[ValueKey] = CultureInfo.CurrentUICulture.Name;
#endif
}
文章 "View Location Expander in ASP.NET Core and MVC 6"提供了一个例子。这是解释的摘录:

You can add as many view location expanders as you want. IViewLocationExpander interface has 2 methods, PopulateValues and ExpandViewLocations. PopulateValues method allows you to add values that can be later consumed by ExpandViewLocations method. The values you put in PopulateValues method will be used to find cache key. ExpandViewLocations method will be only invoked if there is no cache result for the cache key or when framework is unable to find the view at the cached result. In the ExpandViewLocations method, you can return your dynamic view locations. Now you can register this view location expander in Startup.cs file,

services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new MyViewLocationExpander());
});

关于asp.net-core-mvc - Asp.Net Core MVC 中的 IViewLocationExpander.PopulateValues() 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36802661/

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