C# - swagger插入值并将字符串数组传递给Linq显示NULL而不是插入的值

时间:2017-05-05 11:21:24

标签: c# asp.net linq api swagger

在Swagger中我插入一些值,我想将包含这些值的字符串数组传递给Linq。 (在这种情况下,ID是字符串)。如果我传递一个简单的字符串,而不是一个数组,它可以插入一个值。但我需要传递一系列值。

这是在尝试插入2个值时由API构建的链接(对于数组来说看起来很奇怪):http://localhost:port/api/Members?ids=97882831302&ids=97882831308

问题是数组显示NULL 而不是插入的值。我收到以下错误:

  

" ExceptionMessage":"无法创建类型的null常量值   ' System.String []&#39 ;.仅实体类型,枚举类型或基元   在此上下文中支持类型。"

enter image description here

public class MembersController : ApiController
    {
    public HttpResponseMessage GetAllMembersByIdentifiers(string[] ids) {
                using (sampleEntities entities = new sampleEntities()){
                    var numbers = entities.tblmembers
                        .Where(p => ids.Contains(p.identify)  ).ToList();

                    if (numbers != null)
                    {
                        return Request.CreateResponse(HttpStatusCode.OK, numbers);
                    }
                    else
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Member with id: " + ids + " not found");
                    }
                }
            }
}

3 个答案:

答案 0 :(得分:0)

o认为您的代码中存在2个问题..

FIRST:

传递数据,如:

bzgrep

第二

在你的C#中,我可以建议你检查字符串是否为空或像

一样空
98989794, //<---
4343545345

答案 1 :(得分:0)

[FromUri]在我的案例中做了修复。集合参数类型可以是IEnumerable<...>

令人困惑的是,默认情况下,其他参数都是从URL正确填充的 - 由于它是一个获取请求,因此IMO也应该发生在收集参数中。

您可以通过检查Request.RequestUri.AbsoluteUri来检查参数是否通过。

使用[FromUri]

with FromUri

没有[FromUri]

without FromUri

答案 2 :(得分:-1)

解决了什么问题:

  1. 添加{federico-scamuzzi暗示的[FromUri]

  2. string[]替换为List<string>

  3. 所以在代码中看起来像这样:

    public HttpResponseMessage FindMemebers([FromUri]List<string> ids) { ...