【问题标题】:May I pass list as a parameter in web api method?我可以在 web api 方法中将列表作为参数传递吗?
【发布时间】:2016-11-08 15:38:45
【问题描述】:
public static Document GetDocument(List<long> IdList)
{
          //my code
}

我从 UI 传递参数为:-

 getMulipleDocument: function (documentId) {
            return $http({
                url: apiUrl + "Info/GetDocument?IdList=" + documentId,});

Bug:- web api 方法中的参数为 null。 我可以将列表作为 Web api 方法中的参数传递吗?

【问题讨论】:

标签: asp.net-web-api


【解决方案1】:

对您的代码进行一些修改。使用 FromUri 构建您的列表

public static Document GetDocument([FromUri]List<long> IdList)
{
          //my code
}

像这样构建网址

var requestUrl = apiUrl + "Info/GetDocument?IdList=" + id1 + "&IdList=" + id2; 

构建requestUrl 类似查询字符串,以?IdList= 开头的第一个ID,然后使用&amp;IdList= 将下一个ID 附加到url

在上述情况下,您也只能传递一个 id,但只能使用 ?IdList=,然后是您的 id

【讨论】:

    猜你喜欢
    • 2015-12-29
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2021-02-02
    • 1970-01-01
    相关资源
    最近更新 更多