MVC JsonResult驼峰系列化
我试图让我的动作返回一个JsonResult,其中所有的属性都在驼峰。
I am trying to make my action return a JsonResult where all its properties are in camelCase.
我有一个简单的模型:
public class MyModel
{
public int SomeInteger { get; set; }
public string SomeString { get; set; }
}
和一个简单的控制器动作:
And a simple controller action:
public JsonResult Index()
{
MyModel model = new MyModel();
model.SomeInteger = 1;
model.SomeString = "SomeString";
return Json(model, JsonRequestBehavior.AllowGet);
}
调用这个动作方法现在返回一个包含下列数据的JsonResult:
Calling this action method now returns a JsonResult containing the following data:
{"SomeInteger":1,"SomeString":"SomeString"}
有关我的应用我需要的行动驼峰返回数据,在某种程度上是这样的:
For my uses i need the action return the data in camelCase, somehow like this:
{"someInteger":1,"someString":"SomeString"}
有没有优雅的方式来做到这一点?
Is there any elegant way to do this?
我正在研究在这里可能的解决方案,并发现MVC3 JSON序列化:如何控制属性名称在那里设置数据成员定义到模型的每一个属性,但我真的不希望这样做?
I was looking into possible solutions around here and found MVC3 JSON Serialization: How to control the property names? where they set DataMember definitions to every property of the model, but I do not really want to do this.
此外,我发现,他们说,这是可能的解决正是这种问题的一个链接:http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#json_camelcasing.它说:
Also I found a link where they say that it is possible to solve exactly this kind of issue: http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#json_camelcasing. It says:
要写入骆驼外壳JSON属性的名称,而不改变你的数据模型,设置串行的CamelCasePropertyNamesContractResolver:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
在这个博客上http://frankapi.word$p$pss.com/2012/09/09/going-camelcase-in-asp-net-mvc-web-api/也mentiones这个解决方案,并指出你可以简单地把它添加到 RouteConfig.RegisterRoutes 以解决此问题。我试了一下,但我不能使它工作。
One entry on this blog http://frankapi.wordpress.com/2012/09/09/going-camelcase-in-asp-net-mvc-web-api/ also mentiones this solution and states you can simply add it to the RouteConfig.RegisterRoutes to fix this issue. I tried it, but I couldn't make it work.
难道你们有什么想法,我是做错了什么?
Do you guys have any idea where I was doing something wrong?
控制器的JSON函数只是包装器创建JsonResults:
The Json functions of the Controller are just wrappers for creating JsonResults:
protected internal JsonResult Json(object data)
{
return Json(data, null /* contentType */, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
}
protected internal JsonResult Json(object data, string contentType)
{
return Json(data, contentType, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
}
protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)
{
return Json(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
}
protected internal JsonResult Json(object data, JsonRequestBehavior behavior)
{
return Json(data, null /* contentType */, null /* contentEncoding */, behavior);
}
protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior)
{
return Json(data, contentType, null /* contentEncoding */, behavior);
}
protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
JsonRequestBehavior = behavior
};
}
JsonResult内部使用的JavaScriptSerializer,所以你不要有关于序列化进程控制:
JsonResult internally uses JavaScriptSerializer, so you don't have control about the serialisation process:
public class JsonResult : ActionResult
{
public JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.DenyGet;
}
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public JsonRequestBehavior JsonRequestBehavior { get; set; }
/// <summary>
/// When set MaxJsonLength passed to the JavaScriptSerializer.
/// </summary>
public int? MaxJsonLength { get; set; }
/// <summary>
/// When set RecursionLimit passed to the JavaScriptSerializer.
/// </summary>
public int? RecursionLimit { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
}
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
if (MaxJsonLength.HasValue)
{
serializer.MaxJsonLength = MaxJsonLength.Value;
}
if (RecursionLimit.HasValue)
{
serializer.RecursionLimit = RecursionLimit.Value;
}
response.Write(serializer.Serialize(Data));
}
}
}
您必须创建自己的JsonResult,写自己的Json控制器功能(如果你需要/想这一点)。您可以创建新的或改写现有的,它是由你。这链接也可能会感兴趣。
You have to create your own JsonResult and write your own Json Controller functions (if you need / want that). You can create new ones or overwrite existing ones, it is up to you. This link might also interest you.