Return camelCased JSON from Web API

前端 未结 6 1072
攒了一身酷
攒了一身酷 2020-12-31 11:42

I\'m trying to return camel cased JSON from an ASP.Net Web API 2 controller. I created a new web application with just the ASP.Net MVC and Web API bits in it. I hijacked t

6条回答
  •  别那么骄傲
    2020-12-31 12:16

    Try this also.

    [AllowAnonymous]
    [HttpGet()]
    public HttpResponseMessage GetAllItems(int moduleId)
    {
                HttpConfiguration config = new HttpConfiguration();
                config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
    
                try
                {
                    List itemList = GetItemsFromDatabase(moduleId);
                    return Request.CreateResponse(HttpStatusCode.OK, itemList, config);
                }
                catch (System.Exception ex)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
                }
    }
    

提交回复
热议问题