剃刀引擎未找到视图 [英] Razor engine not finding views

查看:79
本文介绍了剃刀引擎未找到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个.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:

无法找到视图"/Views/Emails/NewOrder/NewOrder.cshtml".这搜索了以下位置:/Views/Emails/NewOrder/NewOrder.cshtml'

'Unable to find view '/Views/Emails/NewOrder/NewOrder.cshtml'. The following locations were searched: /Views/Emails/NewOrder/NewOrder.cshtml'

我已将 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);

这篇关于剃刀引擎未找到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆