web api返回格式小结
web api返回格式小结:
1.默认是返回xml格式数据,如果需要返回json格式,需要在Global.asax中加入:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
2.当以string数据类型返回时,默认是加双引号,如果需要将双引号去掉,需要将string类型转换并以HttpResponseMessage类型返回,
string result = "***";
HttpResponseMessage result_return = new HttpResponseMessage();
result_return = new HttpResponseMessage { Content = new StringContent(result, Encoding.GetEncoding("UTF-8"), "text/plain") };
以json格式返回:
Result_return = new HttpResponseMessage { Content = new StringContent(result, Encoding.GetEncoding("UTF-8"), "application/json") };