剃刀引擎找不到视图 [英] Razor engine cant find view

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

问题描述

我试图在不使用Web请求的情况下从视图呈现html.我需要HTML作为字符串,在内部,我不希望提供它.

Im trying to render a html from a view without using a web request. I need the HTML as a string, internally, i do not wish to serve it.

viewEngine.FindView()返回一个viewEnineResult,显示未找到任何视图.它显示要搜索的位置,看起来像这样:

The viewEngine.FindView() returns a viewEnineResult that shows no view was found. It shows to search locations where it looked they look like this:

/Views//PDFOperationsReportView.cshtml

/Views//PDFOperationsReportView.cshtml

/Views/Shared/PDFOperationsReportView.cshtml

/Views/Shared/PDFOperationsReportView.cshtml

(请注意第一行中的双正斜杠)

(Observe the double forward slash in the first line)

文件结构(我将其放入HTML代码段中,因为无法在此编辑器中正确设置文本格式)

File structure (I placed it into a HTML snippet cause i couldnt manage to format the text properly in this editor)

Project 
  Folder 
    Subfolder
        CodeFile.cs
  Views
    PDFOperationsReportView.cshtml

代码:

   var viewName = "PDFOperationsReportView";
        var actionContext = GetActionContext();
        var viewEngineResult = _viewEngine.FindView(actionContext, viewName, false);
        if (!viewEngineResult.Success)
        {
            throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", viewName));
        }

        var view = viewEngineResult.View;

推荐答案

我遇到了同样的问题.我在这里找到了答案: GitHub aspnet/Mvc问题#4936

I had the same issue. I found the answer here: GitHub aspnet/Mvc Issue #4936

基本上,使用GetView代替FindView,如下所示:

Basically, use GetView instead of FindView, like this:

var viewResult = razorViewEngine.GetView(viewName, viewName, false);

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

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