servicestackservicestack-razor

ServiceStack Render Razor Fails to Find View


This is a self hosted project. There is a Views\Member.cshtml file that is set to copy always as content. The following when run returns null for the razorView. I seem to be missing something here.

    var razorView = razor.GetViewPage("Member"); //e.g. /Views/Member.cshtml
    var html = razor.RenderToHtml(razorView, em);
    Console.WriteLine(html);

Furthermore I've tried this and it returns file not found although the file is there:

var html = razor.RenderToHtml(AppDomain.CurrentDomain.BaseDirectory + 
"Views\\" + "Member.cshtml", em);

Also, is there a way to have a service gateway return the rendered razor?

Member.cshtml exists: https://db.tt/xuOSAjEj31

razor: https://db.tt/qeApkAEZGH

AppHost.cs:

  Plugins.Add(new RazorFormat() {
        //    ScanRootPath = AppDomain.CurrentDomain.BaseDirectory + "Views"
        } );

Solution

  • I've tested this with the ServiceStack Self Host with Razor project in ServiceStackVS after changing each .cshtml to Copy if newer, changed AppHost.Configure() to just:

    public override void Configure(Container container)
    {
        this.Plugins.Add(new RazorFormat());
    }
    

    Then added the HelloView Service below:

    [Route("/views/hello/{Name}")]
    public class HelloView
    {
        public string Name { get; set; }
    }
    
    public class MyServices : Service
    {
        public object Any(Hello request)
        {
            return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) };
        }
    
        public object Any(HelloView request)
        {
            var razor = HostContext.GetPlugin<RazorFormat>();
            var helloView = razor.GetViewPage("Hello");
            var response = Any(request.ConvertTo<Hello>());
            var html = razor.RenderToHtml(helloView, response);
            return html;
        }
    }
    

    Which works as expected where razor.GetViewPage("Hello") returns the view in Views\Hello.cshtml and it returns the generated html.

    Also, is there a way to have a service gateway return the rendered razor?

    The Service Gateway is used to call Services which return Typed Response DTOs, not generated html views. You can use base.Gateway to access the Service Gateway in Razor Views.