ASP.NET Core Web API无法按路径查找cshtml文件

时间:2019-06-08 06:09:52

标签: c# asp.net-core asp.net-core-webapi asp.net-core-2.1

我想在我的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);

文件在这里:

enter image description here

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

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

此代码可以正常运行:

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

可以使用文件路径,但是View中的viewResult仍然为空

当我尝试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中,路径正确:

enter image description here

enter image description here

删除.cshtml扩展名后,SearchedLocations为空

3 个答案:

答案 0 :(得分:3)

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

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解决方案中的嵌入式资源而产生的。 class library folder structure

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

好处是,发布解决方案时,它会使解决方案变平,因此VIEWS位于ROOT文件夹中。

publish

[解决方案]

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

从这里获得我的解决方案: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));
            });

此后,您可以像下面这样放置代码:

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

答案 1 :(得分:0)

我没有找到类似的问题,所以我发布了可行的解决方案。

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

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

一切正常

答案 2 :(得分:0)

尝试使用FindView,但我遇到了同样的问题,并已解决。像这样  var viewResult = _razorViewEngine.FindView(actionContext, viewName, false); 希望可以帮到您!