c# - ASP.NET Core 无法使用 RazorViewEngine 通过路径找到 cshtml 文件
问题描述
我想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);
文件在这里:
但是从上面的代码中,View
(即~/PdfTemplate/ProjectRaport.cshtml
)为空。
如何通过路径查找特定文件WebApi Core
?
此代码工作正常:
var viewResult = this.razorViewEngine.FindView(actionContext,
Path.Combine(this.hostingEnvironment.ContentRootPath, "PdfTemplate", "ProjectRaport.cshtml"),
false);
文件路径没问题,但View
inviewResult
仍然为空
当我尝试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
是空的
解决方案
我最近尝试实现 Scott Sauber 解释的剃刀电子邮件模板
问题是我必须克服一个错误(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 解决方案中。
奇怪的是,如果根文件夹中没有文件,您仍然会得到CACHED
Layout 页面。
好的部分是,当您发布解决方案时,它会将解决方案展平,因此VIEWS
位于ROOT
文件夹中。
[解决方案]
解决方案似乎在 Startup.cs 文件夹中。
从这里得到我的解决方案:找不到位于引用项目中的视图
//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);
推荐阅读
- node.js - 我尝试解决这个问题,但我得到错误 var dbo = db.db('mydb') is not a function 请检查错误和代码(Node js 和 mongo db)
- r - 根据第二列日期和时间更改列值的名称
- css - 变换比例看不到并排
- python - 使用条件从 DataFrame python 打印 XML 文件
- java - Springboot 中的 Resilience4J 未按预期工作
- cloud - SAC 链接维度未更新
- flutter - 如何在 Flutter Web 中使用 URL 参数?
- android - 将 android.content.Context 传递给挂起的函数会导致 android.os.NetworkOnMainThreadException
- powershell - Powershell:我需要清理一组 csv 文件,在导入之前必须经过的标题上方的垃圾行数不一致
- ios - 您在 Firebase 完成处理程序中会遇到哪些类型的错误?