以编程方式呈现ServiceStack Razor视图

时间:2014-03-19 18:17:50

标签: servicestack

我正在尝试在服务器上以编程方式呈现ServiceStack Razor页面(因此我可以通过电子邮件发送)。我正在关注https://groups.google.com/forum/#!topic/servicestack/RqMnfM73ic0帖子上的信息,但是当我使用cshtml文件的有效路径调用“AddPage”方法时,它会失败。

var response = svc.Get(oReq);

        var razor = TryResolve<RazorFormat>();
        var path = @"C:\GetOrderResponse.cshtml";
        var razorPage = razor.AddPage(path);

这会抛出一个带有消息的参数异常:

第二个路径片段不能是驱动器或UNC名称。 参数名称:path2

 at System.IO.Path.InternalCombine(String path1, String path2)
 at System.IO.FileSystemEnumerableIterator`1.GetFullSearchString(String fullPath, String        searchPattern)
  at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
  at System.IO.DirectoryInfo.InternalGetDirectories(String searchPattern, SearchOption searchOption)
at ServiceStack.VirtualPath.FileSystemVirtualDirectory.EnumerateDirectories(String dirName)
at ServiceStack.VirtualPath.FileSystemVirtualDirectory.GetDirectoryFromBackingDirectoryOrDefault(String dName)
at ServiceStack.VirtualPath.AbstractVirtualDirectoryBase.GetFile(Stack`1 virtualPath)
at ServiceStack.VirtualPath.AbstractVirtualDirectoryBase.GetFile(String virtualPath)
at ServiceStack.VirtualPath.AbstractVirtualPathProviderBase.GetFile(String virtualPath)
at ServiceStack.VirtualPath.MultiVirtualPathProvider.GetFile(String virtualPath)
at ServiceStack.Razor.Managers.RazorViewManager.GetVirutalFile(String ospath)
at ServiceStack.Razor.Managers.RazorViewManager.AddPage(String filePath)
at ServiceStack.Razor.RazorFormat.AddPage(String filePath)

1 个答案:

答案 0 :(得分:7)

如果它位于/Views/下,您可能不想添加应该已添加的页面。相反,您可以按名称解析现有页面,并使用以下内容将其呈现为html:

var razor = HostContext.GetPlugin<RazorFormat>();
var orderPage = razor.GetViewPage("GetOrderResponse");
email.BodyHtml = razor.RenderToHtml(orderPage, order);