【发布时间】:2020-07-14 17:04:29
【问题描述】:
在我的 asp.net 核心项目中,我尝试使用此方法查找 Razor 视图:
private IView FindView(ActionContext actionContext, string viewName)
{
var getViewResult = _viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
if (getViewResult.Success)
{
return getViewResult.View;
}
var findViewResult = _viewEngine.FindView(actionContext, viewName, isMainPage: true);
if (findViewResult.Success)
{
return findViewResult.View;
}
var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
var errorMessage = string.Join(
Environment.NewLine,
new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations));
throw new InvalidOperationException(errorMessage);
}
在哪里
viewName = "Views/Email/ResetPassword.cshtml"
而_viewEngine 是IRazorViewEngine,但没有找到。
我的项目结构:
IView.FindView 方法是从业务中调用的。
我还有另一个项目,它具有项目结构并使用相同的方法来检索视图,更重要的是,它找到了这个视图,但它使用 netcoreapp2.2,而我当前的项目使用 netcoreapp3.1(Microsoft. AspNetCore.Mvc.Razor 版本相同 - 2.2.0)。
为什么这个方法在 .net core 3.1 上找不到视图?
更新
两个项目都会在构建时将此 Views 文件夹复制到 Api\bin\Debug\netcoreapp{version} 文件夹。
【问题讨论】:
-
请创建一个minimal reproducible example。假设
_viewEngine是 Razor 视图引擎,据我记得,在搜索视图时需要去掉文件扩展名(此处为.cshtml)。所以我不相信这适用于另一个项目。 -
@CodeCaster 因为它被提到了here,所以没有必要关闭扩展。所以它起作用了。
-
@KainWhite 如果您在调用
GetView时设置isMainPage: false);会发生什么?看起来在FindView中您只需传递视图的名称而不需要目录信息,我想这种方法不需要扩展名。 -
@RyanWilson 仍然没有找到它。
.FindView()相同 -
@KainWhite 您是否尝试查找任何其他视图以查看是否可以找到它们?如果您也找不到其他视图,我想您的代码有问题。
标签: asp.net-core asp.net-core-mvc razorengine