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

查看:184
本文介绍了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仍然为空

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中,路径正确:

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

When I deleted .cshtml extension, SearchedLocations is empty

推荐答案

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

I recently tried to implement the razor email template explained by Scott Sauber

此处的教程:问题是我必须克服一个错误( 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);

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

Getting the bin\Debug\netcoreapp2.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;
    }
}

一切正常,直到我对模板文件有疑问为止.问题是,即使文件位于~\bin\Debug\netcoreapp2.2中,它也会在根文件夹(如rootfolder\Views\Shared\_Layout.cshtml)中搜索模板,而不是在rootfolder\bin\Debug\netcoreapp2.2\Views\Shared\_Layout.cshtml中.

All fine and dandy until I had issues regarding the template file. Problem was that even though the file is in ~\bin\Debug\netcoreapp2.2 it searches for the template in the root folder such as rootfolder\Views\Shared\_Layout.cshtml and not in rootfolder\bin\Debug\netcoreapp2.2\Views\Shared\_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.

好的一面是,发布解决方案时,它会使解决方案变平,因此VIEWSROOT文件夹中.

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天全站免登陆