public static class WebApiConfig{public static void Register(HttpConfiguration config){config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional });
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);}}
public static class WebApiConfig{public static void Register(HttpConfiguration config){// ... possible routing etc.
// Setup to return json and camelcase it!var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;formatter.SerializerSettings.ContractResolver =new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();}
WebApiConfig.cs
namespace com.yourstartup{using ...;using System.Net.Http.Formatting;...config.Formatters.Clear(); //because there are defaults of XML..config.Formatters.Add(new JsonMediaTypeFormatter());}
public static void Register(HttpConfiguration config) {config.MessageHandlers.Add(new ForceableContentTypeDelegationHandler());}
或按路线划分:
config.Routes.MapHttpRoute(name: "SpecialContentRoute",routeTemplate: "api/someUrlThatNeedsSpecialTreatment/{id}",defaults: new { controller = "SpecialTreatment" id = RouteParameter.Optional },constraints: null,handler: new ForceableContentTypeDelegationHandler());
public static void Register(HttpConfiguration config){// Web API configuration and services
// Web API routesconfig.MapHttpAttributeRoutes();//Below formatter is used for returning the Json result.var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);//Default routeconfig.Routes.MapHttpRoute(name: "ApiControllerOnly",routeTemplate: "api/{controller}");}
public static class WebApiConfig{public static void Register(HttpConfiguration config){//add this two lineconfig.Formatters.Clear();config.Formatters.Add(new JsonMediaTypeFormatter());
............................}}
public static class WebApiConfig{public static void Register(HttpConfiguration config){...
// Configure Web API to return JSONconfig.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
...}}
public class JsonOutputAttribute : ActionFilterAttribute{public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext){ObjectContent content = actionExecutedContext.Response.Content as ObjectContent;var value = content.Value;Type targetType = actionExecutedContext.Response.Content.GetType().GetGenericArguments()[0];
var httpResponseMsg = new HttpResponseMessage{StatusCode = HttpStatusCode.OK,RequestMessage = actionExecutedContext.Request,Content = new ObjectContent(targetType, value, new JsonMediaTypeFormatter(), (string)null)};
actionExecutedContext.Response = httpResponseMsg;base.OnActionExecuted(actionExecutedContext);}}
适用于行动:
[JsonOutput]public IEnumerable<Person> GetPersons(){return _repository.AllPersons(); // the returned output will be in JSON}
public static void Register(HttpConfiguration config){config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "{controller}/{id}",defaults: new { id = RouteParameter.Optional });config.Formatters.Remove(config.Formatters.XmlFormatter);config.EnableSystemDiagnosticsTracing();}