Pasar múltiples parámetros al método GET de la API web
He creado unWEB API
utilizandoMySQL
Base de datos. La API funciona como se espera por ahora. Envié un número de serie del medidor y un parámetro de fecha y hora y luego OBTENER el resultado esperado. Abajo está mi controlador
public MDCEntities medEntitites = new MDCEntities();
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);
}
}
Abajo está miWebApiConfig
archivo
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}$" }
);
La URL eshttp://localhost:14909/api/meters/GetByMsn/002999000171/2017-10-10T10:08:20
La respuesta que obtengo es
[{
"MSN": "002999000171",
"DateTime": "2017-10-10T10:04:39",
"Signal_Strength": "20"
},
{
"MSN": "002999000171",
"DateTime": "2017-10-10T10:06:35",
"Signal_Strength": "19"
},
{
"MSN": "002999000171",
"DateTime": "2017-10-10T10:08:31",
"Signal_Strength": "20"
},
{
"MSN": "002999000171",
"DateTime": "2017-10-10T10:10:27",
"Signal_Strength": "20"
},
{
"MSN": "002999000171",
"DateTime": "2017-10-10T10:12:23",
"Signal_Strength": "20"
}]
Todo este escenario funciona cuando unsingle serial number
esta pasado. Pero en el lado del cliente habría más de un número de serie diferente. Para esto, tuve que hacer que mi método funcionara para uno y más de un número de serie, siempre que la fecha y hora sea la misma para todos.
One solution is to create a new method a pass the multiple serial number strings, but this will not help because the number of serial numbers are dynamic i.e. they may be one, two to 100's. So setting a hard coded method won't be a solution.
Lo he buscado, pero la mayoría de las veces he encontrado el método estático una y otra vez. Pero estosolución Parece algo útil, pero de nuevo no sé si funcionará o no.
Cualquier ayuda sería muy apreciada.