使用RazorViewToStringRenderer和ServiceStack渲染剃刀页面

时间:2020-06-01 18:06:30

标签: macos razor .net-core 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\work\3481147c480f4a2f\src\ServiceStack\ServiceStackHost.cs:line 282
at ServiceStack.NetCoreAppHostExtensions.UseServiceStack(IApplicationBuilder app, AppHostBase appHost) in C:\BuildAgent\work\3481147c480f4a2f\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中找到该仓库。仅出于测试目的,我试图通过应用Startup类来呈现模板。

我可以从常规.net核心应用程序渲染模板,而不能从ServiceStack应用程序渲染模板。可以在here上找到常规.net核心应用程序上的有效示例的存储库。渲染是在index page

上执行的

1 个答案:

答案 0 :(得分:1)

这些示例不一样,您是从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,因此应将其呈现在MVC控制器或Razor页面中,而不是具有自己的Razor实现以用于ServiceStack Services的ServiceStack AppHost或Service。使用自己的stand-alone Razor APIs。如果您对使用 ServiceStack.Razor 感兴趣,请参考razor项目模板以获取有效的配置。

尽管它比Razor更简单,更干净,更灵活,并且实际上是设计用于stand-alone Sandbox的,所以您还应该考虑使用ServiceStack #Script来渲染独立模板,这里是{{3 }}。