ASP.NET Web API在Firefox下总显示为XML格式,如何切换为JSON格式?


看了一下ASP.net Web API,发现使用Web API创建RESTful服务比WCF还是方便很多了。因为我使用的是Firefox下的一个插件RESTClient做测试,在使用Firefox测试时发现,返回的结果总是XML格式的,而不是我想要的JSON格式,如果使用IE的话倒是JSON格式。原因在于Firefox默认的header信息Accept是Application/xml,而IE是Application/json. 于是对应的解决办法就是添加Header

Accept:application/json

ASP.NET Web API在Firefox下总显示为XML格式,如何切换为JSON格式?

还有一个办法就是在服务器段更改,让web API忽略客户端的设置,在global.asax.cs里面加上GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();


  protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();


WebApiConfig.Register(GlobalConfiguration.Configuration);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

RouteConfig.RegisterRoutes(RouteTable.Routes);

BundleConfig.RegisterBundles(BundleTable.Bundles);


GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();


}


作者:峻祁连
邮箱:[email protected] 
出处:http://junqilian.cnblogs.com 
转载请保留此信息。




本文转自峻祁连. Moving to Cloud/Mobile博客园博客,原文链接:http://www.cnblogs.com/junqilian/archive/2013/02/16/2913414.html,如需转载请自行联系原作者