我试图在不使用Web请求的情况下从视图中呈现html.我需要HTML作为字符串,在内部,我不希望提供它.
viewEngine.FindView()返回一个viewEnineResult,显示未找到任何视图.它显示搜索它看起来像这样的位置:
/Views//PDFOperationsReportView.cshtml
/Views/Shared/PDFOperationsReportView.cshtml
(观察第一行中的双正斜杠)
文件结构(我将其放入HTML片段,因为我无法在此编辑器中正确设置文本格式)
Project
Folder
Subfolder
CodeFile.cs
Views
PDFOperationsReportView.cshtmlRun Code Online (Sandbox Code Playgroud)
代码:
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;
Run Code Online (Sandbox Code Playgroud) 在我的 asp.net core 项目中,我尝试使用以下方法查找 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);
}
Run Code Online (Sandbox Code Playgroud)
在哪里
viewName = "Views/Email/ResetPassword.cshtml"
Run Code Online (Sandbox Code Playgroud)
是_viewEngine,IRazorViewEngine但没有找到任何。
我的项目结构:
IView.FindView方法是从 Business …