ASP MVC 5 and Json.NET: action return type

后端 未结 5 2055
闹比i
闹比i 2021-01-19 23:14

i\'m using ASP MVC 5. I have an action in a controller that return a json object:

[HttpGet]
public JsonResult GetUsers()
{
  return Json(....., JsonRequestBe         


        
5条回答
  •  面向向阳花
    2021-01-19 23:55

    You basically need to write a custom ActionResult that is indicated here in this post

    [HttpGet]
    public JsonResult GetUsers()
    {
        JObject someData = ...;
        return new JSONNetResult(someData);
    }
    

    The JSONNetResult function is:

    public class JSONNetResult: ActionResult
    {
         private readonly JObject _data;
         public JSONNetResult(JObject data)
         {
             _data = data;
         }
    
         public override void ExecuteResult(ControllerContext context)
         {
             var response = context.HttpContext.Response;
             response.ContentType = "application/json";
             response.Write(_data.ToString(Newtonsoft.Json.Formatting.None));
         }
     }
    

提交回复
热议问题