Let the Web API support the $ format parameter method, apiformat
1 public static class WebApiConfig 2 {3 public static void Register (HttpConfiguration config) 4 {5 // Web API configuration and service 6 7 // Web API Route 8 config. mapHttpAttributeRoutes (); 9 10 config. routes. mapHttpRoute (11 name: "DefaultApi", 12 routeTemplate: "api/{controller}/{id}", 13 defaults: new {id = RouteParameter. optional} 14); 15 16 config. routes. mapHttpRoute (17 name: "ActionApi", 18 routeTemplate: "{controller}/{action}/{id}", 19 defaults: new {id = RouteParameter. optional} 20); 21 22 config. formatters. jsonFormatter. addQueryStringMapping ("$ format", "json", "application/json"); 23 config. formatters. xmlFormatter. addQueryStringMapping ("$ format", "xml", "application/xml"); 24 25} 26}
Delete XML
1 public static class WebApiConfig 2 {3 public static void Register (HttpConfiguration config) 4 {5 // Web API configuration and service 6 7 // Web API Route 8 config. mapHttpAttributeRoutes (); 9 10 config. routes. mapHttpRoute (11 name: "DefaultApi", 12 routeTemplate: "api/{controller}/{id}", 13 defaults: new {id = RouteParameter. optional} 14); 15 16 config. routes. mapHttpRoute (17 name: "ActionApi", 18 routeTemplate: "{controller}/{action}/{id}", 19 defaults: new {id = RouteParameter. optional} 20); 21 22 config. formatters. xmlFormatter. supportedMediaTypes. clear (); 23} 24}
Usage
Http: // localhost: 8081/api/home? $ Format = json
Http: // localhost: 8081/api/home? $ Format = xml
References: https://code.msdn.microsoft.com/Support-format-in-ASPNET-e3785b2a