0
我在visual studio 2015中創建了一個API
。運行API時,它會按預期給我提供響應和數據。下面是我的控制器代碼在web API中更改json結果
public HttpResponseMessage GetByMsn(string msn, DateTime dt)
{
try
{
var before = dt.AddMinutes(-5);
var after = dt.AddMinutes(5);
var result = medEntitites.tj_xhqd
.Where(m =>
m.zdjh == msn &&
m.sjsj >= before &&
m.sjsj <= after).Select(m => new { MSN = m.zdjh, DateTime = m.sjsj, Signal_Strength = m.xhqd }).Distinct();
return Request.CreateResponse(HttpStatusCode.Found, result);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
的WebApiConfig
文件低於
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Routes.MapHttpRoute(
name: "GetByMsn",
routeTemplate: "api/{controller}/{action}/{msn}/{dt}",
defaults: null,
constraints: new { msn = @"^[0-9]+$" , dt = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$" }
);
的響應是
[
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:16:51",
"Signal_Strength": "17"
},
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:19:01",
"Signal_Strength": "16"
},
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:20:57",
"Signal_Strength": "16"
},
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:22:53",
"Signal_Strength": "17"
}
]
我想
{"list":
[
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:16:51",
"Signal_Strength": "17"
},
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:19:01",
"Signal_Strength": "16"
},
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:20:57",
"Signal_Strength": "16"
},
{
"MSN": "002999000077",
"DateTime": "2017-10-11T10:22:53",
"Signal_Strength": "17"
}]
}
我只是想加上list
名稱中的首發。我是API的新手,所以我找不到方法。
任何幫助將不勝感激。