WebApi接口返回json,xml,text纯文本等

[Route("api/Message/MessageList/")]
[HttpGet]
public HttpResponseMessage MessageList()
{
    ResponseResult obj = new ResponseResult();
    var pageData = Ydt.ExtLib.DataTableHelper.DataTableToList(result.Tables[0]);
    List list = new List();
    foreach (var item in pageData)
    {
        list.Add(JObject.FromObject(item));
    }
    obj.info = list;
    obj.status = true;
    obj.message = "获取成功";
    JsonStr = JsonConvert.SerializeObject(obj);
    //返回纯文本text/plain  ,返回json application/json  ,返回xml text/xml
    HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(JsonStr, Encoding.GetEncoding("UTF-8"), "application/json") };
    return result;
}

你可能感兴趣的:(MVC)