Created
August 6, 2014 12:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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