최근 JSON.net 계약 해법을 사용할 수 있도록 Controller 클래스의 Json 메서드를 재정의하려는 중 이상한 시나리오를 발견했습니다. 결국 ContentResult 객체를 반환하고 ActionResult로 업 캐스트하면 완벽하게 작동합니다. JSONResult 객체를 반환하려고하면 작동하지 않습니다.재정의 된 Json 메서드에서 JSONResult를 반환하는 것이 작동하지 않고 ContentResult가 작동하는 이유는 무엇입니까?
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;
}
설명이 있습니까?
왜 안 되니? 무슨 일이야? – SLaks