ASP.NET Core 无法使用 RazorViewEngine 按路径找到 cshtml 文件 [英] ASP.NET Core cannot find cshtml file by path using RazorViewEngine

查看:33
本文介绍了ASP.NET Core 无法使用 RazorViewEngine 按路径找到 cshtml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to find my cshtml file in my ASP.NET Core 2.1 Web API project. To do it, I'm using this code:

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)是空的.

But from code above, the View (that is ~/PdfTemplate/ProjectRaport.cshtml) is null.

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

How to find specific file by path in WebApi Core?

此代码工作正常:

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

文件路径没问题,但是viewResult中的View还是null

Path to file is ok, but the View in viewResult is still null

当我尝试 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 仍然为空

编辑

SearchedLocations 中,路径没问题:

In SearchedLocations the path is ok:

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

When I deleted .cshtml extension, SearchedLocations is empty

推荐答案

我最近尝试实现 Scott Sauber 解释的 razor 电子邮件模板

I recently tried to implement the razor email template explained by 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/

问题是我必须克服一个错误(https://stackoverflow.com/a/56504181/249895) 需要程序集所在的相关 netcodeapp2.2.

Issue was that I had to overcome a bug(https://stackoverflow.com/a/56504181/249895) that needed the relative netcodeapp2.2 that the asemblied resided in.

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

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

获取binDebug etcoreapp2.2 可以使用如下代码实现:

Getting the binDebug etcoreapp2.2 can be achieved by using this code:

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;
    }
}

一切都很好,直到我遇到有关模板文件的问题.问题是,即使文件在 ~inDebug etcoreapp2.2 中,它也会在根文件夹中搜索模板,例如 rootfolderViewsShared\_Layout.cshtml 而不是 rootfolderinDebug etcoreapp2.2ViewsShared\_Layout.cshtml.

All fine and dandy until I had issues regarding the template file. Problem was that even though the file is in ~inDebug etcoreapp2.2 it searches for the template in the root folder such as rootfolderViewsShared\_Layout.cshtml and not in rootfolderinDebug etcoreapp2.2ViewsShared\_Layout.cshtml.

这很可能是由于我将视图作为 CLASS LIBRARY 中的嵌入式资源而不是直接在 Web Api 解决方案中生成的.

This is most likely generated by the fact that I have the views as an embedded resource in a CLASS LIBRARY and not in a Web Api solution directly.

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

The weird part is that if you do not have the files in the root folder, you still get the CACHED Layout page.

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

The good part is that when you PUBLISH the solution, it flattens the solution so the VIEWS are in ROOT folder.

[解决方案]

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

The solution seems to be in the Startup.cs folder.

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

Got my solution from here: Cannot find view located in referenced project

//https://stackoverflow.com/q/50934768/249895
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));
            });

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

After this, you can put your code like this:

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);

这篇关于ASP.NET Core 无法使用 RazorViewEngine 按路径找到 cshtml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆