サーチ…


前書き

ASP.NET MVCでは、ビューはデフォルトでViewsフォルダに配置されます。場合によっては、この場所を変更して別の場所にビューを保存することがあります。

ビューのロケーションエキスパンダーを作成する

ビューの位置を変更できるようにするには、 IViewLocationExpanderを実装する必要があります。 ExpandViewLocationsメソッドは、検索する場所が異なるIEnumerable<string>を返します。

public class MyViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        yield return "/CustomViewFolder/{1}/{0}.cshtml";
        yield return "/SharedFolder/{0}.cshtml";
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {            
    }
}

ビューのロケーションエキスパンダーを登録する

Razor View EngineでExpanderを使用するには、Expanderを登録する必要があります。 StartupクラスのConfigureServicesにこれを追加するだけです。

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


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow