Linq 向web API GET方法传递多个参数
我使用Linq 向web API GET方法传递多个参数,linq,rest,get,asp.net-web-api2,asp.net-web-api-routing,Linq,Rest,Get,Asp.net Web Api2,Asp.net Web Api Routing,我使用MySQL数据库创建了一个webapi。API目前的工作情况与预期一致。我发送了一个仪表序列号和一个日期时间参数,然后得到了预期的结果。下面是我的控制器 public MDCEntities medEntitites = new MDCEntities(); public HttpResponseMessage GetByMsn(string msn, DateTime dt) { try { var be
MySQL
数据库创建了一个webapi
。API目前的工作情况与预期一致。我发送了一个仪表序列号和一个日期时间参数,然后得到了预期的结果。下面是我的控制器
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);
}
}
URL是http://localhost:14909/api/meters/GetByMsn/002999000171/2017-10-10T10:08:20
我得到的答复是
[{
"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"
}]
当传递一个单一序列号时,此所有方案均有效。但在客户端,会有不止一个不同的序列号。为此,我必须使我的方法既适用于一个序列号,也适用于多个序列号,前提是所有序列号的日期和时间都相同
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.
我已经搜索过了,但是大多数时候我一次又一次地找到了静态方法。但这看起来有些帮助,但我也不知道它是否会起作用
任何帮助都将不胜感激 您可以将自定义模型传递给操作方法,但我建议不要为您的任务使用GET
,因为GET
没有主体
相反,使用SEARCH
动词,将序列号和日期列表放在主体中的自定义模型中
public class MeterSearchModel
{
public List<string> Serials {get;set;}
public DateTime Date {get;set;}
}
公共类MeterSearchModel
{
公共列表序列{get;set;}
公共日期时间日期{get;set;}
}
在.NET Core 2中,您的控制器将具有以下功能:
[AcceptVerbs("SEARCH")]
public async Task<IActionResult> Search([FromBody] MeterSearchModel model)
{
//..perform search
}
[接受动词(“搜索”)]
公共异步任务搜索([FromBody]MeterSearchModel)
{
//…执行搜索
}
[AcceptVerbs("SEARCH")]
public async Task<IActionResult> Search([FromBody] MeterSearchModel model)
{
//..perform search
}