Skip to content

Instantly share code, notes, and snippets.

@CoreyKaylor
Created November 7, 2014 19:52
Show Gist options
  • Save CoreyKaylor/4c97d9317b5108e43f90 to your computer and use it in GitHub Desktop.
Save CoreyKaylor/4c97d9317b5108e43f90 to your computer and use it in GitHub Desktop.
public class ViewDataForActivation
{
[Activate]
public ViewDataDictionary ViewData { get; set; }
}
public class ViewOutputFormatter : Microsoft.AspNet.Mvc.OutputFormatter
{
public ViewOutputFormatter()
{
SupportedEncodings.Add(System.Text.Encoding.UTF8);
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/html"));
}
public override Task WriteResponseBodyAsync(OutputFormatterContext context)
{
var activator = (IControllerActivator)context.ActionContext.HttpContext.RequestServices.GetService(typeof(IControllerActivator));
var forActivation = new ViewDataForActivation();
activator.Activate(forActivation, context.ActionContext);
var viewData = forActivation.ViewData;
viewData.Model = context.Object;
var result = new ViewResult();
result.ViewData = viewData;
return result.ExecuteResultAsync(context.ActionContext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment