没有弄乱它,无法给你一个具体的答案,但请查看 ASP.NET MVC GitHub 存储库上的 IViewLocationExpander.PopulateValues(ViewLocationExpanderContext context):
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
}
可读性格式:
“由RazorViewEngine 调用以确定IViewLocationExpander 的此实例将使用的值。计算的值用于确定视图位置自上次定位以来是否已更改。
参数:
context:当前视图位置扩展操作的ViewLocationExpanderContext。"
我查看了一些实现此接口的类 - 一些声明了该方法但将其留空,另一些则实现了它。
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" 提供了一个示例。以下是解释的摘录:
您可以根据需要添加任意数量的视图位置扩展器。 IViewLocationExpander 接口有 2 个方法,PopulateValues 和 ExpandViewLocations。 PopulateValues 方法允许您添加以后可以由 ExpandViewLocations 方法使用的值。您在PopulateValues 方法中输入的值将用于查找缓存键。 ExpandViewLocations 方法只会在没有缓存键的缓存结果或框架无法在缓存结果中找到视图时调用。在ExpandViewLocations 方法中,您可以返回动态视图位置。现在您可以在Startup.cs 文件中注册此视图位置扩展器,
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new MyViewLocationExpander());
});