问题描述
我目前有一个.NET Core库,可用于将Razor页面呈现为HTML电子邮件.我正在追踪本教程.在编译时没有错误,但是在运行时出现以下错误:
I currently have a .NET Core library that I am using to render Razor pages as HTML emails. I am following this tutorial. There are no errors during compile-time but at runtime I am getting the following error:
我已将 NewOrder.cshtml
的构建操作设置为 Content
,并且已将输出目录的副本设置为 Copy Always
.我不确定为什么无法找到视图,正如我在 bin \ Debug \ netcoreapp2.2 \ Views
文件夹中看到的那样,将电子邮件复制到了输出目录.
I have set the build action of NewOrder.cshtml
to Content
and I have set the copy to output directory to Copy Always
. I am unsure to why this is unable to find the view as I can see in the bin\Debug\netcoreapp2.2\Views
folder that the emails are being copied to the output directory.
我正在使用以下代码搜索视图:
I am searching for the view with the following code:
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);
}
推荐答案
似乎是在源目录中查找而不是实际执行的程序集文件夹.我通过将 FindView
方法的第一行更改为以下内容来修复它:
It seemed that it was looking in the source directory instead of the actually executing assembly folder. I fixed it by changing the first line of the FindView
method to the following:
var dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var getViewResult = _viewEngine.GetView(executingFilePath: dir, viewPath: viewName, isMainPage: true);
这篇关于剃刀引擎未找到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!