How can My Asp.Net C# class return a json format

前端 未结 3 1477
心在旅途
心在旅途 2020-12-06 04:45

How would like a class that will return a json format.

This method work Great in the controller but when I want to put in a Class, the Json object don\'t seem to exi

相关标签:
3条回答
  • 2020-12-06 05:22

    Json() is a method on the base controller which returns a JsonResult. You need to do the serialization yourself.

    return new JavaScriptSerializer().Serialize(new { errMsg = "test" });
    

    You will need to include using System.Web.Script.Serialization.

    0 讨论(0)
  • 2020-12-06 05:42

    return Json(new { errMsg = "test"});

    is a convenience method on Controller that is equivalent to

    return new JsonResult(){
          Data = new { errMsg = "test"},
          JsonRequestBehavior = JsonRequestBehavior.DenyGet
    };
    
    0 讨论(0)
  • 2020-12-06 05:43

    For me this worked (mind the change of the return type to Object):

    public Object Test()
    {
          return new { errMsg = "test" };
    }
    
    0 讨论(0)
提交回复
热议问题