Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a ServiceStack Razor view programmatically

Tags:

servicestack

I am trying to render a ServiceStack Razor page programmatically on the server (so I can send it via email). I am following the info on https://groups.google.com/forum/#!topic/servicestack/RqMnfM73ic0 post, but when I call the "AddPage" method with a valid path for the cshtml file, it falls over.

var response = svc.Get(oReq);

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

This throws an Argument Exception with the message:

Second path fragment must not be a drive or UNC name. Parameter name: 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)
like image 697
Gumzle Avatar asked Mar 19 '14 18:03

Gumzle


1 Answers

You likely don't want to add the page which should already be added if it's under /Views/. Instead you can resolve the existing page by name and render it to html with:

var razor = HostContext.GetPlugin<RazorFormat>();
var orderPage = razor.GetViewPage("GetOrderResponse");
email.BodyHtml = razor.RenderToHtml(orderPage, order);
like image 57
mythz Avatar answered Nov 16 '22 02:11

mythz