Passing parameter list to Umbraco API plugin controller action

时间:2016-04-25 09:44:03

标签: c# umbraco

How to pass a list of values to an Umbraco API plugin controller action?

I have the following action in my controller:

namespace web.site.Controllers 
{
    [PluginController("MyObject")]
    public class MyObjectApiController : UmbracoAuthorizedJsonController
    {
        public MyObjectApiController()
        { }

        public void Delete(List<int> ids) {
            foreach (var id in ids)
            {
                // ....
            }
        }
    }
}

I cannot find the right "wire format" for the ids parameter. I've tried the following:

  • ids[0]=123&ids[1]=456;
  • ids[]=123&ids[]=456;
  • ids=123&ids=456.

Every time the action is called but ids is null. What is the right way to serialize a list of values for the action parameter?

1 个答案:

答案 0 :(得分:3)

您需要在控制器操作参数之前添加[FromUri],例如......

Delete([FromUri]List<int> ids)

让模型绑定器知道如何从Uri中的参数创建id列表。

您发送请求时,您尝试的所有格式都可以使用

  • ids[0]=123&ids[1]=456
  • ids[]=123&ids[]=456
  • ids=123&ids=456