" Microsoft.AspNet.Mvc":" 6.0.0-rc1-final"自定义视图引擎

时间:2016-03-04 22:21:10

标签: model-view-controller asp.net-core-mvc viewengine

我在" Microsoft.AspNet.Mvc":" 6.0.0-rc1-final"和 我再也看不到AddMvc上的configure方法了,AddViewOptions.ViewEngine会抛出

services.AddMvc().AddViewOptions(o =>
           {
               o.ViewEngines.Add(typeof(ICANRazorViewEngine));
               o.ViewEngines.Clear();
           });

让我无法将System.Type转换为IViewEngine?

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

在撰写本文时(2016年3月10日),"Creating a Custom View Engine" (Page 299)的官方ASP.NET文档无法使用。

我使用" Microsoft.AspNet.Mvc":" 6.0.0-rc1-final"得到同样的错误,但因为我的意图只是包含其他视图位置,我修复它:

public class CustomViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
    }

    public virtual IEnumerable<string> ExpandViewLocations(
        ViewLocationExpanderContext context,
        IEnumerable<string> viewLocations)
    {
        return viewLocations.Union(new string[] { "~/Views/{1}/PartialViews/{0}.cshtml" });
    }
}

并将代码添加到Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddMvc()
            .AddRazorOptions(options =>
            {
                options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
            })
            .AddViewLocalization(LanguageViewLocationExpanderFormat.SubFolder);
    }

我希望能以某种方式帮助你。