I'm trying to create an attribute that will serialize data return from an action differently
public override void OnActionExecuted(HttpActionExecutedContext filterContext)
{
var content = (filterContext.Response.Content as ObjectContent);
if (content == null)
{
return;
}
if (content.ObjectType.IsGenericType
&& content.ObjectType.GetGenericTypeDefinition() == typeof (Page<>))
{
var pageObject = (content.Value as IPage);
var jsonFormatterRule = new JsonFormatterRule();
var pageJson = JsonConvert.SerializeObject(pageObject.ItemsArray,
jsonFormatterRule.GetPascalCasedSettings());
//How do I set the content that \/ doesn't compile?
//filterContext.Response.Content = pageJson;
}
}
This is the JsonFormatterRules incase anyone wanted to see them.
public JsonSerializerSettings GetDefaultSettings()
{
var settings = new JsonSerializerSettings()
{
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind,
};
settings.Converters.AddRange(defaultConfiguredConverters);
return settings;
}
public JsonSerializerSettings GetPascalCasedSettings()
{
var settings = this.GetDefaultSettings();
settings.ContractResolver = new DefaultContractResolver();
return settings;
}
How can I Set the Content From On Action Executed? I cannot change the default serializer to the DefaultContract Globally because it could threading issues.
Also I'd prefer not to have to create a new response and copy over the Headers from the old one that seems like over kill.