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

mic*_*cer 3 c# razor asp.net-core asp.net-core-2.1

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

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);
Run Code Online (Sandbox Code Playgroud)

该文件在这里:

在此处输入图片说明

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

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

这段代码工作正常:

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

文件的路径是确定的,但ViewviewResult仍然是空

当我尝试GetView

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

viewResult.View仍然为空

编辑

SearchedLocations路径中是可以的:

在此处输入图片说明

在此处输入图片说明

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

Dra*_*lut 7

我最近尝试实现 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/

问题是我必须克服一个错误(/sf/answers/3955292701/),该错误需要组装所在的相关 netcodeapp2.2。

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

_viewEngine.GetView(executingFilePath: viewPath , viewPath: viewPath , isMainPage: true);
Run Code Online (Sandbox Code Playgroud)

获取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;
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,直到我遇到有关模板文件的问题。问题是,即使该文件是在~\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 文件夹中。

从这里得到我的解决方案:找不到位于引用项目中的视图

///sf/ask/3565433791/
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));
            });
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)