我有以下
- dotnet core 6 控制台应用程序
- dotnet core 6 Razor 类库(带有 .cshtml)
我正在做一个在 web api 中工作得很好的 View Engine RenderAsync()。在控制台应用程序中运行时找不到模板,模板在 dll 中。
第一个问题,该cshtml的相对/绝对路径是什么?我正在使用:@"Templates/Pdf/LevelsEmail.cshtml"
控制台应用启动
IServiceCollection serviceCollection = new ServiceCollection();
services.AddScoped<ITemplateBuilder, TemplateBuilder>();
var listener = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton(listener);
services.AddSingleton<DiagnosticSource>(listener);
services.AddRazorPages();
// rest of deps
var scope = provider.CreateScope();
await scope.ServiceProvider.GetRequiredService<Runner>().ProcessAsync();
在 RCL csproj
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
尝试 GetView 或 FindView
var httpContext = new DefaultHttpContext
{
RequestServices = _serviceProvider
};
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
搜索 ViewEngineResult
var viewResult = _viewEngine.GetView(executingFilePath: viewPath, viewPath: viewPath, isMainPage);
if (getViewResult.Success)
{
return getViewResult;
}
var findViewResult = _viewEngine.FindView(actionContext, viewPath, isMainPage);
if (findViewResult.Success)
{
return findViewResult;
}
然后渲染(这只是一个例子,问题在于获取视图)
var viewContext = new ViewContext(actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(httpContext, _tempDataProvider),
outputWriter,
new HtmlHelperOptions());
await viewResult.View.RenderAsync(viewContext);
第二个问题:这可以在控制台应用程序(而不是网络应用程序)中使用 RazorViewEngine 吗?