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
290 views
in Technique[技术] by (71.8m points)

asp.net - C# - swagger inserting values and passing array of strings to Linq shows NULL instead of the values inserted

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");
                    }
                }
            }
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

[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


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

...