C# - swagger inserting values and passing array of

2019-09-15 09:59发布

In Swagger I insert some values and I want to pass an array of strings, containing those values, to Linq. (the ids are strings in this case). If I pass a simple string, not an array, it works to insert one value. But I need to pass an array of values.

Here's a link built by the API when trying to insert 2 values (it looks weird for an array): http://localhost:port/api/Members?ids=97882831302&ids=97882831308

The problem is that the array shows NULL instead of the values inserted. And I get the following error:

"ExceptionMessage": "Unable to create a null constant value of type 'System.String[]'. Only entity types, enumeration types or primitive types are supported in this context."

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条回答
Summer. ? 凉城
2楼-- · 2019-09-15 10:22

o think you've 2 problems in your code ..

FIRST:

pass data with , like:

98989794, //<---
4343545345

SECOND:

in your C# can i suggest to you to check for string null or empty like:

 public class MembersController : ApiController
 {
     [HttpGet]
     [Route("api/Members/Find/{ids}")]
     public HttpResponseMessage FindMemebers([FromUri]string[] ids) {
        // CHECK THE ARRAY NOT EMPTY
        if(!ids.Any() || ids.All(xx=> string.isNullOrEmpty(xx))
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "empty params");

        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楼-- · 2019-09-15 10:35

[FromUri] did the fix in my case. Collection parameter type can be IEnumerable<...>.

It's confusing that other parameters are filled correctly from a URL by default - which IMO should also happen to collection parameters as it's a get request.

You could check whether the parameters are passed by inspecting Request.RequestUri.AbsoluteUri.

With [FromUri]:

with FromUri

Without [FromUri]:

without FromUri

查看更多
小情绪 Triste *
4楼-- · 2019-09-15 10:42

What solved it was:

  1. adding [FromUri] hinted by @federico-scamuzzi

  2. replacing string[] with List<string>.

So in the code looks like this:

public HttpResponseMessage FindMemebers([FromUri]List<string> ids) { ...
查看更多
登录 后发表回答