问题描述
我试图在不使用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/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);
这篇关于剃刀引擎找不到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!