ASP.NET Core 2.0 ViewLocation - 如何添加区域

时间:2018-03-12 16:34:50

标签: asp.net-core-2.0

我在我的项目中添加了一个名为/ Areas / Views / Shared的区域 (仅供参考...这是对视图/共享默认值的补充)

我相信我在Startup中添加了正确的服务(在services.AddMvc之后添加)

Startup.cs
        services.Configure<RazorViewEngineOptions>(options => options.ViewLocationFormats.Add("/Areas/Views/Shared/" + RazorViewEngine.ViewExtension));

我收到以下错误:

InvalidOperationException: The partial view 'Areas/Views/Shared/_foo' was not found. 
The following locations were searched:
/Views/Shared/_foo.cshtml

如果_foo.cshtml不存在,那么我希望该异常的内容如下:

InvalidOperationException: The partial view 'Areas/Views/Shared/_foo' was not found. 
The following locations were searched:
/Views/Shared/_foo.cshtml
/Areas/Views/Shared/_foo.cshtml

我阅读了几个帖子,例如下面的项目......但我暂时陷入困境

How to specify the view location in asp.net core mvc when using custom locations?

1 个答案:

答案 0 :(得分:0)

经过更多的游戏,我找到了解决方案......我错过了{0}

Startup.cs
     services.Configure<RazorViewEngineOptions>(options => options.ViewLocationFormats.Add("/Areas/Views/Shared/{0}" + RazorViewEngine.ViewExtension));