ASP.NET Core 无法使用 RazorViewEngine 按路径找到 cshtml 文件

ASP.NET Core cannot find cshtml file by path using RazorViewEngine

我想在我的 ASP.NET Core 2.1 Web API 项目中找到我的 cshtml 文件。为此,我使用了以下代码:

var httpContext = new DefaultHttpContext { RequestServices = this.serviceProvider };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

var viewResult = this.razorViewEngine.FindView(
                actionContext,
                "~/PdfTemplate/ProjectRaport.cshtml",
                false);

文件在这里:

但是从上面的代码来看,View(即~/PdfTemplate/ProjectRaport.cshtml)为空。

如何在 WebApi Core 中通过路径查找特定文件?

此代码工作正常:

var viewResult = this.razorViewEngine.FindView(actionContext,
                Path.Combine(this.hostingEnvironment.ContentRootPath, "PdfTemplate", "ProjectRaport.cshtml"),
                false);

文件路径没问题,但 viewResult 中的 View 仍然为空

当我尝试时 GetView:

var viewResult = this.razorViewEngine.GetView(
                Path.Combine(this.hostingEnvironment.ContentRootPath, "PdfTemplate", "ProjectRaport.cshtml"),
                Path.Combine(this.hostingEnvironment.ContentRootPath, "PdfTemplate", "ProjectRaport.cshtml"),
                false);

viewResult.View 仍然是 null

编辑

SearchedLocations中路径没问题:

当我删除 .cshtml 扩展时,SearchedLocations 是空的

我没有找到对我的问题有效的答复,所以我发布了有效的解决方案。

不要将 PathCombineContentRootPath 一起使用,只需键入:

string viewPath = "~/PdfTemplate/ProjectRaport.cshtml";
var viewResult = this.razorViewEngine.GetView(viewPath, viewPath, false);

它工作正常

我最近尝试实施 Scott Sauber 解释的剃须刀电子邮件模板

教程在这里:https://scottsauber.com/2018/07/07/walkthrough-creating-an-html-email-template-with-razor-and-razor-class-libraries-and-rendering-it-from-a-net-standard-class-library/

问题是我必须克服一个 bug(),它需要 asemblied 所在的相关 netcodeapp2.2。

var viewPath = ‘~/bin/Debug/netcoreapp2.2/Views/MainView.cshtml’;

_viewEngine.GetView(executingFilePath: viewPath , viewPath: viewPath , isMainPage: true);

获取 bin\Debug\netcoreapp2.2 可以使用以下代码实现:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
public class RenderingService : IRenderingService
{

    private readonly IHostingEnvironment _hostingEnvironment;
    public RenderingService(IHostingEnvironment hostingEnvironment)
    {
    _hostingEnvironment = hostingEnvironment;
    }

    public string RelativeAssemblyDirectory()
    {
        var contentRootPath = _hostingEnvironment.ContentRootPath;
        string executingAssemblyDirectoryAbsolutePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        string executingAssemblyDirectoryRelativePath = System.IO.Path.GetRelativePath(contentRootPath, executingAssemblyDirectoryAbsolutePath);
        return executingAssemblyDirectoryRelativePath;
    }
}

在我遇到有关模板文件的问题之前一切都很好。问题是即使文件在 ~\bin\Debug\netcoreapp2.2 中,它也会在根文件夹中搜索模板,例如 rootfolder\Views\Shared\_Layout.cshtml 而不是在 rootfolder\bin\Debug\netcoreapp2.2\Views\Shared\_Layout.cshtml.

这很可能是因为我在 CLASS LIBRARY 而不是直接在 Web Api 解决方案中将视图作为嵌入式资源。

奇怪的是,如果您没有根文件夹中的文件,您仍然会看到 CACHED 布局页面。

好的部分是,当您发布解决方案时,它会将解决方案展平,因此 VIEWS 位于 ROOT 文件夹中。

[解决方案]

解决方案似乎在 Startup.cs 文件夹中。

从这里得到我的解决方案:Cannot find view located in referenced project

//
services.Configure<Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions>(o => {
                o.ViewLocationFormats.Add("/Views/{0}" + Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.ViewExtension);
                o.FileProviders.Add(new Microsoft.Extensions.FileProviders.PhysicalFileProvider(AppContext.BaseDirectory));
            });

在此之后,您可以这样放置代码:

var contentRootPath = _hostingEnvironment.ContentRootPath;
string executingAssemblyDirectoryAbsolutePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string executingAssemblyDirectoryRelativePath = System.IO.Path.GetRelativePath(contentRootPath, executingAssemblyDirectoryAbsolutePath);

string executingFilePath = $"{executingAssemblyDirectoryAbsolutePath.Replace('\', '/')}/Views/Main.cshtml";
string viewPath = "~/Views/Main.cshtml";
string mainViewRelativePath = $"~/{executingAssemblyDirectoryRelativePath.Replace('\','/')}/Views/Main.cshtml";

var getViewResult = _viewEngine.GetView(executingFilePath: executingFilePath, viewPath: viewPath, isMainPage: true);

<!-- OR -->

var getViewResult = _viewEngine.GetView(executingFilePath: viewPath, viewPath: viewPath, isMainPage: true);