Asp.net Web API 返回Json對象的兩種方式

這兩種方式都是以HttpResponseMessage的形式返回,json

方式一:以字符串的形式app

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

方式二:以對象(這裏用的是字典)的方式orm

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);
相關文章
相關標籤/搜索