Ich habe eine API
in Visual Studio 2015 erstellt. Während der API gibt es mir die Antwort und Daten wie erwartet. Unten ist mein Controller-CodeÄndern der Json-Ergebnis in Web-API
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);
}
}
Die WebApiConfig
Datei unter
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}$" }
);
ist Die Antwort ist
[
{
"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"
}
]
Und ich will
{"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"
}]
}
bekommen Ich möchte nur Fügen Sie diehinzuName im Start. Ich bin neu in den APIs, so kann ich den Weg nicht finden.
Jede Hilfe würde sehr geschätzt werden.