Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Created August 6, 2014 12:25
Show Gist options
  • Save lkaczanowski/9c3f0fc484eb9400c310 to your computer and use it in GitHub Desktop.
Save lkaczanowski/9c3f0fc484eb9400c310 to your computer and use it in GitHub Desktop.
Implementation of ASP.NET Web.Api IHttpActionResult that returns html rendered by Razor
public class HtmlActionResult<T> : IHttpActionResult
{
private readonly string _viewTemplate;
private readonly T _model;
public HtmlActionResult(string viewTemplateFullPath, T model)
{
_viewTemplate = File.ReadAllText(viewTemplateFullPath);
_model = model;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
var parsedView = RazorEngine.Razor.Parse(_viewTemplate, _model);
response.Content = new StringContent(parsedView);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return Task.FromResult(response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment