Razor engine cant find view

蓝咒 提交于 2019-12-19 18:28:07

问题


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.

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/Shared/PDFOperationsReportView.cshtml

(Observe the double forward slash in the first line)

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

The code:

   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;

回答1:


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

Basically, use GetView instead of FindView, like this:

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


来源:https://stackoverflow.com/questions/43996438/razor-engine-cant-find-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!