看了一下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
還有一個辦法就是在服務器段更改,讓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();
}