Last active
March 21, 2017 16:44
-
-
Save DanielToft/33cd26e0ddfc0088d69c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| using System; | |
| using System.Net.Http; | |
| using System.Net.Http.Formatting; | |
| using System.Web.Http.Filters; | |
| using Newtonsoft.Json.Serialization; | |
| namespace Filters | |
| { | |
| [AttributeUsage(AttributeTargets.All)] | |
| public class CamelCasingFilterAttribute : ActionFilterAttribute | |
| { | |
| private static readonly JsonMediaTypeFormatter _camelCasingFormatter = new JsonMediaTypeFormatter(); | |
| static CamelCasingFilterAttribute() | |
| { | |
| _camelCasingFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); | |
| } | |
| public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) | |
| { | |
| if (actionExecutedContext == null) return; | |
| if (actionExecutedContext.Response == null) return; | |
| var content = actionExecutedContext.Response.Content as ObjectContent; | |
| if (content == null) return; | |
| if (content.Formatter is JsonMediaTypeFormatter) { | |
| actionExecutedContext.Response.Content = new ObjectContent(content.ObjectType, content.Value, _camelCasingFormatter); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment