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

爷,独闯天下 提交于 2019-11-28 09:47:54

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.

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
};

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

public Object Test()
{
      return new { errMsg = "test" };
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!