What is IViewLocationExpander.PopulateValues() for in Asp.Net Core MVC

大憨熊 提交于 2019-12-22 04:58:11

问题


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.

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)

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.


回答1:


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

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.

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

  • 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

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.

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.

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.

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.




回答2:


Haven't messed around with it enough to be able to give you a concrete answer, but have a look at IViewLocationExpander.PopulateValues(ViewLocationExpanderContext context) on the ASP.NET MVC GitHub repo:

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
}

Readability format:

"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."

I've had a look at some classes which implement this interface - some declare the method but leave it empty, others implement it.

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
}

The article "View Location Expander in ASP.NET Core and MVC 6" provides an example. Here's an excerpt of the explanation:

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());
});



回答3:


Basically the method can populate values into context.Values that will later be used to determine if a cached list should be used or if the ExpandViewLocations will be called....



来源:https://stackoverflow.com/questions/36802661/what-is-iviewlocationexpander-populatevalues-for-in-asp-net-core-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!