Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

linq - Passing multiple parameters to web API GET method

I have created a WEB API using MySQL Database. The API works as expected for now. I sent a meter serial number and a date time parameter and then GET the expected result. Below is my controller

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);
        }
    }

Below is my WebApiConfig file

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}Td{2}:d{2}:d{2}$" }
       );

The URL is http://localhost:14909/api/meters/GetByMsn/002999000171/2017-10-10T10:08:20

The response I GET is

[{
    "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"
}]

This all scenario works when a single serial number is passed. But at client side there would be more than one different serial numbers. For this I had to make my method to work both for one and more than one serial numbers provided the date time will be the same for all.

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. 

I have searched for it but most of the times I have found the static method again and again. But this solution looks some what helpful but again I don't know whether it will work or not.

Any help would be highly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can pass a custom model to an action method, but I suggest not using the GET for you task because GET does not have a body.

Instead, use the SEARCH verb and put the list of serials number and the date inside a custom model in the body.

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

In .NET Core 2 your controller would have something like -

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...