从网页API返回JSON驼峰 [英] Return camelCased JSON from Web API
问题描述
这是一个ASP.Net的Web API 2控制器套管JSON。我创建了只是其中的ASP.Net MVC和Web API位一个新的Web应用程序。我劫持了ValuesController像这样:
公共类ValuesController:ApiController
{
公共类的事
{
公众诠释标识{搞定;组; }
公共字符串名字{获得;组; }
公共字符串ISBN {搞定;组; }
公众的DateTime RELEASEDATE {搞定;组; } 公共字符串[] {标签获取;组; }
} //获取API /值
公共IHttpActionResult获得()
{
VAR的事情=新的东西
{
ID = 123,
名字=布莱恩
ISBN =ABC213
RELEASEDATE = DateTime.Now,
标签=新的字符串[] {A,B,C,D}
}; 返回JSON(事);
}
}
在IE中运行这个,我得到如下结果:
<$p$p><$c$c>{\"Id\":123,\"FirstName\":\"Brian\",\"ISBN\":\"ABC213\",\"ReleaseDate\":\"2014-10-20T16:26:33.6810554-04:00\",\"Tags\":[\"A\",\"B\",\"C\",\"D\"]}随着<一个href=\"http://odeto$c$c.com/blogs/scott/archive/2013/03/25/asp-net-webapi-tip-3-camelcasing-json.aspx\">K.关于这个问题的斯科特·艾伦的帖子,我增加了以下在WebApiConfig.cs文件注册方法:
公共静态无效的注册(HttpConfiguration配置)
{
//网页API的配置和服务
VAR格式化= GlobalConfiguration.Configuration.Formatters;
VAR jsonFormatter = formatters.JsonFormatter;
VAR设置= jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver =新CamelCasePropertyNamesContractResolver(); //网页API路线
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
名称:DefaultApi
routeTemplate:API / {}控制器/(编号),
默认:新{ID = RouteParameter.Optional}
);
}
不过,我仍然得到同样的,capitilization在我的结果。有我丢失的东西?我已经尝试了一些其他的方法,但没有什么工作呢。
看起来像的主要问题是,我使用JsonResult快捷JSON()作用的结果方式:
公共IHttpActionResult获取([FromUri]串领域,[FromUri]用户名字符串)
{
VAR AUTHINFO = BLL.GetAuthenticationInfo(域名,用户名);
返回JSON(AUTHINFO);
}
这显然是有格式的结果的完全控制。如果我切换到返回的Htt presponseMessage那么它将按预期工作:
公开的Htt presponseMessage获取([FromUri]串领域,[FromUri]用户名字符串)
{
VAR AUTHINFO = BLL.GetAuthenticationInfo(域名,用户名);
返回Request.CreateResponse(的HTTPStatus code.OK,AUTHINFO);
}
我没有最终使用code块在WebApiConfig文件Omar.Alani建议(相对于很多更长code,我在我的OP)。但是,真正的罪魁祸首是JsonResult操作方法。我希望这可以帮助别人了。
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 the ValuesController like so:
public class ValuesController : ApiController
{
public class Thing
{
public int Id { get; set; }
public string FirstName { get; set; }
public string ISBN { get; set; }
public DateTime ReleaseDate { get; set; }
public string[] Tags { get; set; }
}
// GET api/values
public IHttpActionResult Get()
{
var thing = new Thing
{
Id = 123,
FirstName = "Brian",
ISBN = "ABC213",
ReleaseDate = DateTime.Now,
Tags = new string[] { "A", "B", "C", "D"}
};
return Json(thing);
}
}
Running this in IE, I get the following results:
{"Id":123,"FirstName":"Brian","ISBN":"ABC213","ReleaseDate":"2014-10-20T16:26:33.6810554-04:00","Tags":["A","B","C","D"]}
Following K. Scott Allen's post on the subject, I added the following to the Register method in the WebApiConfig.cs file:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
However, I still get the same, capitilization in my results. Is there something I'm missing? I've tried a few other approaches but nothing works yet.
Looks like the main issue was that I was using the JsonResult shortcut Json() action result method:
public IHttpActionResult Get([FromUri] string domain, [FromUri] string username)
{
var authInfo = BLL.GetAuthenticationInfo(domain, username);
return Json(authInfo);
}
It apparently had full control of formatting the results. If I switch to returning HttpResponseMessage then it works as expected:
public HttpResponseMessage Get([FromUri] string domain, [FromUri] string username)
{
var authInfo = BLL.GetAuthenticationInfo(domain, username);
return Request.CreateResponse(HttpStatusCode.OK, authInfo);
}
I did end up using the block of code in the WebApiConfig file as Omar.Alani suggested (opposed to the much lengthier code I had in my OP). But the real culprit was the JsonResult action method. I hope this helps someone else out.
这篇关于从网页API返回JSON驼峰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!