MySQL
データベースを使用してWEB API
を作成しました。 APIは今のところ期待どおりに動作します。私はメートルのシリアル番号と日付の時間パラメータを送信し、次に期待される結果を得る。以下は私のWebApiConfig
ファイル私のコントローラの下複数のパラメータをWeb API GETメソッドに渡す
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);
}
}
されている
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}$" }
);
URLは私がGET応答は、このすべてのシナリオはsingle serial number
の作品
[{
"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"
}]
あるhttp://localhost:14909/api/meters/GetByMsn/002999000171/2017-10-10T10:08:20
です合格。しかし、クライアント側では、複数の異なるシリアル番号が存在します。このためには、日付の時間がすべて同じになるという前提で、1つ以上のシリアル番号の両方で動作するように自分のメソッドを作成しなければなりませんでした。
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.
私はそれを検索しましたが、たいていは静的メソッドを何度も見つけました。しかし、このsolutionは役に立つかどうかわかりますが、再びうまくいくかどうかわかりません。
ご協力いただければ幸いです。