赞
踩
背景:webapi和webservice想返回json格式的数据,供前台直接使用。
返回数据样式:
WEBAPI:
接口返回类型为HttpResponseMessage
- public HttpResponseMessage ReturnJson(string content)
- {
- HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(content, Encoding.GetEncoding("UTF-8"), "application/json") };
- return result;
- }
WebService:
接口返回类型为void
- public void ReturnData(object data)
- {
- data = JsonConvert.SerializeObject(data);
- Context.Response.Charset = "UTF-8"; //设置字符集类型
- Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
- Context.Response.Write(data);
- Context.Response.End();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。