C#-迅速插入值并将字符串数组传递给Linq显示NULL而不是插入的值 [英] C# - swagger inserting values and passing array of strings to Linq shows NULL instead of the values inserted
问题描述
在Swagger中,我插入一些值,我想将包含这些值的字符串数组传递给Linq. (在这种情况下,id是字符串).如果我传递一个简单的字符串而不是一个数组,则它可以插入一个值.但是我需要传递一个值数组.
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.
以下是API尝试插入2个值(对于数组来说很奇怪)所建立的链接: http://localhost:port/api/Members?ids = 97882831302& ids = 97882831308
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
问题是数组显示NULL 而不是插入的值.我收到以下错误:
The problem is that the array shows NULL instead of the values inserted. And I get the following error:
"ExceptionMessage":无法创建类型的空常量值 'System.String []'.仅实体类型,枚举类型或原语 在此上下文中支持类型."
"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."
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");
}
}
}
}
推荐答案
[FromUri]
在我的情况下解决了问题.收集参数类型可以为IEnumerable<...>
.
[FromUri]
did the fix in my case. Collection parameter type can be IEnumerable<...>
.
默认情况下,URL正确填充了其他参数,这令人困惑-IMO在获取请求时也应该同时收集参数.
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.
您可以通过检查Request.RequestUri.AbsoluteUri
来检查参数是否通过.
You could check whether the parameters are passed by inspecting Request.RequestUri.AbsoluteUri
.
使用[FromUri]
:
没有[FromUri]
:
这篇关于C#-迅速插入值并将字符串数组传递给Linq显示NULL而不是插入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!