将多个参数传递给Web API GET方法 [英] Passing multiple parameters to web API GET method

查看:166
本文介绍了将多个参数传递给Web API GET方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MySQL 数据库创建了 WEB API 。 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
。其中(m =>
m.zdjh == msn&&
m.sjsj> =之前&&
m.sjsj< = after).Select(m => new {MSN = m.zdjh,DateTime = m.sjsj,Signal_Strength = m.xhqd})。Distinct();


返回Request.CreateResponse(HttpStatusCode.Found,result);
}
catch(Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound,ex);




$ b $ p $以下是我的 WebApiConfig file

  config.Routes.MapHttpRoute(
名称:GetByMsn,
routeTemplate:api / {controller} / {action} / {msn} / {dt},
默认值:null,
约束:new {msn = @^ [0-9] + $,dt = @^ \d {4} -\\d {2} -\\d {2} T \d {2}:\d {2}:\d {2} $ }
);

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



I GET的回应是

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

当传递一个单个序列号时,所有情况都可以使用。但在客户端,会有不止一个不同的序列号。为此,我必须让我的方法可以同时处理一个和多个序列号,前提是日期时间对所有人都是一样的。

 一种解决方法是创建一个传递多个序列号字符串的新方法,但这不会有帮助,因为序列号数字是动态的,即它们可能是一,二到一百。所以设置一个硬编码的方法将不是一个解决方案。 

我已经搜索过它,但大部分时间我都一次又一次地找到了静态方法。但是这个解决方案看起来有些有用,但我不知道它是否会起作用或不。

任何帮助将不胜感激。

解决方案

您可以将自定义模型传递给动作方法,但我建议不要使用 GET 为你的任务,因为 GET 没有正文。



使用 SEARCH 动词并将自定义模型中的连续编号和日期列表放入

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


$ b $在.NET Core 2中,你的控制器会有类似于 - $ /

  [AcceptVerbs(SEARCH)] 
public async Task< IActionResult>搜索([FromBody] MeterSearchModel模型)
{
//....执行搜索
}


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}T\d{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.

解决方案

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 
}

这篇关于将多个参数传递给Web API GET方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆