Asp.net Web API 返回Json对象的两种方式

这两种方式都是以HttpResponseMessage的形式返回,

方式一:以字符串的形式

var content = new StringContent("{\"FileName\": \"" + fileName + "\"}");
HttpResponseMessage response = new HttpResponseMessage()
{
     Content = content
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

方式二:以对象(这里用的是字典)的方式

var fileNames = new Dictionary<string, string>();
fileNames.Add("FileName", fileName); 
var content = new ObjectContent<Dictionary<string, string>>(fileNames, new JsonMediaTypeFormatter(), "application/json");
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, content);