Asp.Net Core MVC中的IViewLocationExpander.PopulateValues()是什么 [英] What is IViewLocationExpander.PopulateValues() for in Asp.Net Core MVC

查看:156
本文介绍了Asp.Net Core MVC中的IViewLocationExpander.PopulateValues()是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC CORE.我已经实现了自己的ViewLocationExpander,以便可以按自己的方式构建项目并将视图放置在自己喜欢的位置.

I'm using ASP.NET MVC CORE. I have implemented my own ViewLocationExpander so that I can structure my project the way I want and place my views where I like.

这是通过实现一个继承自IViewLocationExpander的类来完成的,大部分工作都在以下方法中进行:

This is accomplished by implementing a class that inherits from IViewLocationExpander and most of the work occurs in the following method:

ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)

一切正常,但是界面定义了我不知道如何正确实现的第二种方法:

Everything is working pretty sweet but the interface defines a 2nd method that I don't know how to properly implement:

PopulateValues(ViewLocationExpanderContext context)

我已经在Internet上阅读了有关此接口的文章,但除了说出有关该方法对缓存有何帮助的模棱两可的内容外,没有人真正提供过有关此方法的确切信息.

I've read articles all over the internet about this interface but no one has really provided much info on what exactly this method is for other than saying vague things about how it helps with caching.

如果有人能够解释该框架如何使用此方法,以及如果确实是它的用途,那么我将如何适当地使用它来辅助缓存,我将非常感激.

I'd really appreciate it if someone could explain how this method is used by the framework and how I can use it appropriately to aid caching if that is indeed what it is for.

推荐答案

也许以下其他信息直接从

Maybe the following additional info taken directly from a GitHub MVC issue can answer your question:

缓存在其查找中包括Values词典.除非PopulateValues()方法向ViewLocationExpanderContext.Values添加了不同的信息,否则ExpandViewLocations()方法将仅针对每个原始文件名被调用一次,即,初始信息将从此开始被缓存.

Caching includes the Values dictionary in its lookup. Unless the PopulateValues() method adds distinct information to ViewLocationExpanderContext.Values, the ExpandViewLocations() method will be called just once per original file name i.e. the initial information is cached from then on.

最重要的是,OP构成的特定示例可以帮助您更好地理解,至少这是发生在我身上的事情:

On top of that, the particular example posed by OP can help understand even better, at least that's what happened to me:

  • 他的项目在两个不同的目录下具有相同名称的视图 树木(例如FooBar)
  • 根据当前动作上下文提取的一些数据,要定位的视图应位于这些树中的任意一棵树下
  • His project has views with the same name under two different directory trees (say Foo and Bar)
  • Depending on some data extracted by current action context, the view to locate should be under either one of those trees

PopulateValues()中没有任何代码,视图引擎将要求一次定位视图,然后使用视图标准"数据(例如ControllerNameActionNameArea等) .)以缓存找到视图的实际位置.

Without any code in PopulateValues(), view engine will ask once to locate the view, then use view "standard" data (e.g. ControllerName, ActionName, Area, etc.) in order to cache the actual location where view is found.

因此,在OP情况下,每当需要使用具有相同名称的视图时,一旦缓存了视图位置(例如,从Foo目录树开始),它将始终位于该树中,则无法检测是否在另一棵Bar树中的那棵树应该已经被捡起了.

So, in OP case, once a view location is cached (say e.g. from Foo directory tree) everytime a view with same name is needed it will always be from that tree, there'll be no way to detect if the one in the other Bar tree should have been actually picked up.

OP的唯一方法是通过向Values词典中添加特定的,独特的视图详细信息来自定义PopulateValues():在当前情况下,这些是从当前操作上下文中提取的信息.

The only way for OP is to customize PopulateValues() by adding specific, distinctive view details to Values dictionary: in current scenario, those are the info extracted from current action context.

该附加信息有两种用法:ExpandViewLocations()在被调用时可能会使用它们来确定适当的位置,而视图引擎将在找到后立即使用它们来缓存视图位置.

That additional info are used two-fold: ExpandViewLocations() might use them when invoked in order to determine proper location, while view engine will use them to cache view location once found.

这篇关于Asp.Net Core MVC中的IViewLocationExpander.PopulateValues()是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆