Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Allow a way to modify locations in which views are looked for without having to modify ViewEngine #1039

@pranavkm

Description

@pranavkm

In Mvc 5, the VirtualPathProviderViewEngine that was the base for all view engines that shipped in the box looked for views in a very specific set of locations that were exposed as properties of that type.

The primary way to use customize rules for discovering view locations was by sub typing and changing the result of these properties. This feature work is designed towards finding a nicer way to go about this:

Proposal: View Location Expanders

  1. We start with a set of seed formats: ["/Views/{1}/{0}", "/Views/Shared/{0}"]
  2. We introduce a contract for view location expanders as
interface IViewLocationExpander
{
     IEnumerable<string> ExpandViewLocations(IEnumerable<string> seed, ActionContext context);
}
  1. Individual expanders can add tokens, expand previously inserted tokens etc
public class LocaleViewLocationExpander : IViewLocationExpander
{
     public IEnumerable<string> ExpandViewLocations(IEnumerable<string> seed, ActionContext context)
     {
        var locale = context.RouteData["locale"];
        if (!string.IsNullOrEmpty(locale))
        {
            // Views/{1}/{0}.en-gb
            // Views/Shared/{0}.en-us
            // Views/{1}/{0}
            // Views/Shared/{0}

            return seed.Select(s => areaName + '/' + s)
                       .Concat(seed);
        }
     }
 }
  1. Expanders are registered via Options so they can be re-ordered, removed etc.The RazorViewEngine calls into these expanders to get all possible locations to locate views as part of FindView and FindPartialView.

cc @yishaigalatzer / @loudej

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions