1、Global 中增加json输出
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json"));
1 protected void Application_Start() 2 { 3 AreaRegistration.RegisterAllAreas(); 4 //添加json 解析 使用方法 http://xxx/api/action?json=true 5 GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json")); 6 WebApiConfig.Register(GlobalConfiguration.Configuration); 7 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 8 RouteConfig.RegisterRoutes(RouteTable.Routes); 9 BundleConfig.RegisterBundles(BundleTable.Bundles); 10 }
2、Global 中删除xml解析
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
1 protected void Application_Start() 2 { 3 AreaRegistration.RegisterAllAreas(); 4 WebApiConfig.Register(GlobalConfiguration.Configuration); 5 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 6 RouteConfig.RegisterRoutes(RouteTable.Routes); 7 BundleConfig.RegisterBundles(BundleTable.Bundles); 8 //删除xml的解析 当返回值是string 时 直接返回string不是json对象 9 GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); 10 }
3、指定返回格式
1 新建方法 需要程序集:System.Web.Extensions 2 public static HttpResponseMessage ToJson(Object obj) 3 { 4 String str; 5 if (obj is String || obj is Char) 6 { 7 str = obj.ToString(); 8 } 9 else 10 { 11 var serializer = new JavaScriptSerializer(); 12 str = serializer.Serialize(obj); 13 } 14 var result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; 15 return result; 16 }
调用户方法转换为json对象输出
1 public HttpResponseMessage GetString(string name) 2 { 3 return ToJson(name); 4 }
4、重写默认实现类 所有输出将被重新解析成 json
1 新建JsonContentNegotiator 类 2 public class JsonContentNegotiator : IContentNegotiator 3 { 4 private readonly JsonMediaTypeFormatter _jsonFormatter; 5 public JsonContentNegotiator(JsonMediaTypeFormatter formatter) 6 { 7 _jsonFormatter = formatter; 8 } 9 10 public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters) 11 { 12 var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json")); 13 return result; 14 } 15 }
1 WebApiConfig中使用重写 2 public static void Register(HttpConfiguration config) 3 { 4 config.Routes.MapHttpRoute( 5 name: "DefaultApi", 6 routeTemplate: "api/{controller}/{id}", 7 defaults: new { id = RouteParameter.Optional } 8 ); 9 10 var jsonFormatter = new JsonMediaTypeFormatter(); 11 config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); 12 13 // 取消注释下面的代码行可对具有 IQueryable 或 IQueryable<T> 返回类型的操作启用查询支持。 14 // 若要避免处理意外查询或恶意查询,请使用 QueryableAttribute 上的验证设置来验证传入查询。 15 // 有关详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=279712。 16 //config.EnableQuerySupport(); 17 18 // 若要在应用程序中禁用跟踪,请注释掉或删除以下代码行 19 // 有关详细信息,请参阅: http://www.asp.net/web-api 20 config.EnableSystemDiagnosticsTracing(); 21 }