WebApi 技巧
1.返回参数去掉xml格式,以纯json格式返回。在WebApiConfig.cs文件中配置
public static void Register(HttpConfiguration config) { //设置不以xml格式返回 config.Formatters.Remove(config.Formatters.XmlFormatter); /// config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); }
2.返回json去掉双引号
//JavaScriptSerializer类在System.Web.Extensions.dll中,注意添加这个引用 JavaScriptSerializer serializer = new JavaScriptSerializer(); //JSON序列化 string result = serializer.Serialize(list); //格式化Json引号"" var respRet = new HttpResponseMessage { Content = new StringContent(result, System.Text.Encoding.UTF8, "application/json") }; return respRet;