使用 RazorViewToStringRenderer 和 ServiceStack 渲染剃刀页面
Render razor page with RazorViewToStringRenderer and ServiceStack
我正在尝试呈现剃刀页面以将其作为电子邮件模板发送。我在 Razor 库上添加视图,并尝试使用此 class 从 ServiceStack 项目呈现这些视图。我收到以下错误:
Application startup exception: System.AggregateException: One or more errors occurred. (Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml)
---> System.InvalidOperationException: Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml
at web.RazorTemplates.RazorViewToStringRenderer.FindView(ActionContext actionContext, String viewName) in /Users/herber/Documents/repos/tests/web/web.RazorTemplates/RazorViewToStringRenderer.cs:line 86
at web.RazorTemplates.RazorViewToStringRenderer.RenderViewToStringAsync[TModel](String viewName, TModel model) in /Users/herber/Documents/repos/tests/web/web.RazorTemplates/RazorViewToStringRenderer.cs:line 39
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at web.AppHost.Configure(Container container) in /Users/herber/Documents/repos/tests/ss-razor-library/web/web/Startup.cs:line 65
at ServiceStack.ServiceStackHost.Init() in C:\BuildAgent\work81147c480f4a2f\src\ServiceStack\ServiceStackHost.cs:line 282
at ServiceStack.NetCoreAppHostExtensions.UseServiceStack(IApplicationBuilder app, AppHostBase appHost) in C:\BuildAgent\work81147c480f4a2f\src\ServiceStack\AppHostBase.NetCore.cs:line 333
at web.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /Users/herber/Documents/repos/tests/ss-razor-library/web/web/Startup.cs:line 44
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()
crit: Microsoft.AspNetCore.Hosting.WebHost[6]
Application startup exception
System.AggregateException: One or more errors occurred. (Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml)
---> System.InvalidOperationException: Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml
可以找到该存储库 here. Just for testing purpose I'm trying to render the template form the app Startup class。
我可以从常规 .net 核心应用程序而不是 ServiceStack 应用程序呈现模板。可以在 here. The render is performed on index page
找到有关常规 .net 核心应用程序的工作示例的回购协议
这些示例不一样,您是从 MVC 控制器渲染它的:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IRazorViewToStringRenderer _razorRenderer;
public IndexModel(ILogger<IndexModel> logger, IRazorViewToStringRenderer razorRenderer)
{
_logger = logger;
_razorRenderer = razorRenderer;
}
public void OnGet()
{
var body = _razorRenderer.RenderViewToStringAsync("/Test.cshtml",new TestModel{Message = "World"}).Result;
Console.WriteLine(body);
}
}
与在应用程序初始化之前启动:
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
DefaultRedirectPath = "/metadata",
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
});
var razorRenderer = container.Resolve<IRazorViewToStringRenderer>();
var body = razorRenderer.RenderViewToStringAsync("/Test.cshtml",new TestModel()).Result;
Console.WriteLine(body);
}
因为您尝试使用 MVC 的 Razor 实现而不是 ServiceStack.Razor you should be rendering it inside an MVC Controller or Razor page, not a ServiceStack AppHost or Service which has its own Razor implementation for usage in ServiceStack Services that uses its own stand-alone Razor APIs. If you're interested in using ServiceStack.Razor refer to the razor 项目模板来进行有效配置。
尽管它比 Razor 更简单、更干净、更灵活,而且实际上设计用于 stand-alone Sandbox, you should also consider ServiceStack #Script for rendering stand-alone templates, here's a rendering Email template example。
我正在尝试呈现剃刀页面以将其作为电子邮件模板发送。我在 Razor 库上添加视图,并尝试使用此 class 从 ServiceStack 项目呈现这些视图。我收到以下错误:
Application startup exception: System.AggregateException: One or more errors occurred. (Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml)
---> System.InvalidOperationException: Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml
at web.RazorTemplates.RazorViewToStringRenderer.FindView(ActionContext actionContext, String viewName) in /Users/herber/Documents/repos/tests/web/web.RazorTemplates/RazorViewToStringRenderer.cs:line 86
at web.RazorTemplates.RazorViewToStringRenderer.RenderViewToStringAsync[TModel](String viewName, TModel model) in /Users/herber/Documents/repos/tests/web/web.RazorTemplates/RazorViewToStringRenderer.cs:line 39
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at web.AppHost.Configure(Container container) in /Users/herber/Documents/repos/tests/ss-razor-library/web/web/Startup.cs:line 65
at ServiceStack.ServiceStackHost.Init() in C:\BuildAgent\work81147c480f4a2f\src\ServiceStack\ServiceStackHost.cs:line 282
at ServiceStack.NetCoreAppHostExtensions.UseServiceStack(IApplicationBuilder app, AppHostBase appHost) in C:\BuildAgent\work81147c480f4a2f\src\ServiceStack\AppHostBase.NetCore.cs:line 333
at web.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in /Users/herber/Documents/repos/tests/ss-razor-library/web/web/Startup.cs:line 44
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()
crit: Microsoft.AspNetCore.Hosting.WebHost[6]
Application startup exception
System.AggregateException: One or more errors occurred. (Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml)
---> System.InvalidOperationException: Unable to find view '/Test.cshtml'. The following locations were searched:
/Test.cshtml
可以找到该存储库 here. Just for testing purpose I'm trying to render the template form the app Startup class。
我可以从常规 .net 核心应用程序而不是 ServiceStack 应用程序呈现模板。可以在 here. The render is performed on index page
找到有关常规 .net 核心应用程序的工作示例的回购协议这些示例不一样,您是从 MVC 控制器渲染它的:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IRazorViewToStringRenderer _razorRenderer;
public IndexModel(ILogger<IndexModel> logger, IRazorViewToStringRenderer razorRenderer)
{
_logger = logger;
_razorRenderer = razorRenderer;
}
public void OnGet()
{
var body = _razorRenderer.RenderViewToStringAsync("/Test.cshtml",new TestModel{Message = "World"}).Result;
Console.WriteLine(body);
}
}
与在应用程序初始化之前启动:
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
DefaultRedirectPath = "/metadata",
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
});
var razorRenderer = container.Resolve<IRazorViewToStringRenderer>();
var body = razorRenderer.RenderViewToStringAsync("/Test.cshtml",new TestModel()).Result;
Console.WriteLine(body);
}
因为您尝试使用 MVC 的 Razor 实现而不是 ServiceStack.Razor you should be rendering it inside an MVC Controller or Razor page, not a ServiceStack AppHost or Service which has its own Razor implementation for usage in ServiceStack Services that uses its own stand-alone Razor APIs. If you're interested in using ServiceStack.Razor refer to the razor 项目模板来进行有效配置。
尽管它比 Razor 更简单、更干净、更灵活,而且实际上设计用于 stand-alone Sandbox, you should also consider ServiceStack #Script for rendering stand-alone templates, here's a rendering Email template example。