【发布时间】:2017-01-30 15:04:02
【问题描述】:
最近我在尝试覆盖 Controller 类的 Json 方法时发现了一个奇怪的场景,以便我可以使用 JSON.net 合同解析器。如果最后我返回一个 ContentResult 对象并将其向上转换为 ActionResult,它会完美运行。但是,如果我尝试返回 JSONResult 的对象,它将不起作用。
protected new ContentResult Json(object data, JsonRequestBehavior behaviour = JsonRequestBehavior.DenyGet)
{
var jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
if (Request.RequestType == WebRequestMethods.Http.Get && behaviour == JsonRequestBehavior.DenyGet)
{
throw new InvalidOperationException("GET is not permitted for this request");
}
var jsonResult = new ContentResult
{
Content = JsonConvert.SerializeObject(data, jsonSerializerSettings),
ContentType = "application/json",
};
return jsonResult;
}
对此有什么解释吗?
【问题讨论】:
-
为什么不呢?会发生什么?
标签: c# json ajax asp.net-mvc jsonresult