RazorGenerator in Self-hosted Web API 2

Jul 7, 2014 at 6:57 AM
Edited Jul 7, 2014 at 6:59 AM
I'm trying to use precompiled views in Self-hosted Web API 2 project (without using RazorEngine nuget package). Currently I render a precompiled view with the help of WebViewPageExtensions.Render method from RazorGenerator.Testing namespace. However, layouts do not work.

Is there any better way to do this with RazorGenerator so the layouts would work?

My code - controller:
     public HtmlActionResult Get()
        {
            return new HtmlActionResult<PrecompiledTestView>();
        }
My HtmlActionResult class:
    public abstract class HtmlActionResult : IHttpActionResult
    {
        public abstract Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
    }

    public class HtmlActionResult<T> : HtmlActionResult where T : WebViewPage, new()
    {
        private readonly T _view;
        private readonly dynamic _model;

        public HtmlActionResult()
        {
            _view = new T();
        }

        public HtmlActionResult(dynamic model) : this()
        {
            _model = model;
        }

        public override Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            var parsedView = WebViewPageExtensions.Render((dynamic) _view, _model);
            response.Content = new StringContent(parsedView);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
            return Task.FromResult(response);
        }
    }