gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:01 24 4
gpt4 key购买 nike

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

文件路径没问题,但是viewResult中的View还是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 仍然是 null

编辑

SearchedLocations 中路径是正确的:

enter image description here

enter image description here

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

最佳答案

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

教程在这里: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

这很可能是因为我将 View 作为嵌入资源放在 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);

关于c# - ASP.NET Core 无法使用 RazorViewEngine 按路径找到 cshtml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56503957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com