方法多种,自己目前采用的是自定义返回格式的方法,不需要修改配置文件. 辅助类: public class ApiResponseHelper { public static HttpResponseMessage ToJson(Object obj) { String str; if (obj is String || obj is Char) { str = obj.ToString(); } else { JavaScriptSerializer serializer = new JavaS…
原文链接:https://www.muhanxue.com/essays/2015/01/8623699.html MVC web api 返回JSON的几种方式 1.在WebApiConfig的Register中加入以下代码 config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); 2.在WebApiConfig的Register中加入以下代码 con…
对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Content": true, , "RequestMessage": "sample string 2" } 这是定义的Response类 public class Response<T> //where T : class { public T Conte…