0

我试图在我的 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 响应(姓氏与姓氏)?

4

0 回答 0