2017-10-11 11 views
0

MySQL 데이터베이스를 사용하여 WEB API을 만들었습니다. API는 현재 예상대로 작동합니다. 미터 일련 번호와 날짜 시간 매개 변수를 보낸 다음 예상 결과를 얻습니다. 다음은 내 WebApiConfig 파일 내 컨트롤러 아래웹 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

입니다 통과된다. 그러나 클라이언트 측에서 하나 이상의 다른 일련 번호가있을 것입니다. 이를 위해 나는 하나의 시리얼 넘버와 둘 이상의 시리얼 넘버 모두를 위해 일하는 방법을 만들어야했다. 날짜 시간은 모두 동일 할 것이다.

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은 도움이 될만한 것이 있지만 다시 작동 여부를 알지 못합니다.

도움을 주시면 감사하겠습니다.

답변

0

작업 방법에 사용자 지정 모델을 전달할 수 있지만 GET에는 본문이 없으므로 GET 작업을 사용하지 않는 것이 좋습니다.

대신 SEARCH 동사를 사용하고 일련 번호와 날짜 목록을 본문에 맞춤 모델 안에 넣으십시오. .NET의 핵심에서

public class MeterSearchModel 
{ 
    public List<string> Serials {get;set;} 
    public DateTime Date {get;set;} 
} 

2 컨트롤러 같은 것 -

[AcceptVerbs("SEARCH")] 
public async Task<IActionResult> Search([FromBody] MeterSearchModel model) 
{ 
    //..perform search 
}