如何强制camelCase响应?

时间:2017-05-16 17:39:37

标签: c# asp.net-web-api owin visual-studio-2017 self-host-webapi

我试图在我的ConfigureApp方法中使用以下行强制执行camelCase响应:

        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

整个方法:

        public static void ConfigureApp(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
            appBuilder.UseWebApi(config);
        }

我将从我的控制器返回以下回复:

    public object Get()
    {
        var result = new { name = "alex", number = 1, lastname = "smith" };

        return Ok(result);
    }

我得到的回应是:

{"name":"alex","number":1,"lastname":"smith"}

我期待的回应是:

{"name":"alex","number":1,"lastName":"smith"}

如何强制使用camelCase响应(lastname vs lastName)?

0 个答案:

没有答案