3

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"
        } );
lucuma
  • 18,247
  • 4
  • 66
  • 91

1 Answers1

2

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • I am using razor.GetViewPage outside of a service, I'll refactor and see if that's the issue – lucuma Apr 20 '17 at 00:33
  • I tested your answer in my project and it did not work, `helloView` is always null. I noticed that the `razor.ScanRootPath` is not set to the Debug folder but not sure if that matters. – lucuma Apr 20 '17 at 00:50
  • @lucuma don't know anything about your project, but it's working as expected in a new SelfHost + Razor project. – mythz Apr 20 '17 at 00:55
  • Project is a test project running hangfire via topshelf and calling a local in proc services via a gateway. I have no problem uploading it somewhere and may post it to the forum. – lucuma Apr 20 '17 at 00:56
  • @lucuma If you're using the latest version of ServiceStack, upload it to a new Github repo and I'll take a look. – mythz Apr 20 '17 at 01:05
  • @lucuma Your issue is that you're using an ASP.NET `AppHostBase` base class for a SelfHost project which should inherit `AppSelfHostBase` instead. Also you should have unique names for Views, i.e. there should only be one view page in `/Views` called `Member.cshtml`. – mythz Apr 20 '17 at 02:03
  • Thank you, the `AppHostBase` was the issue. – lucuma Apr 20 '17 at 02:12