Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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