【问题标题】:Posting a List of GUIDs to a MVC 5 Controller with Postman使用 Postman 将 GUID 列表发布到 MVC 5 控制器
【发布时间】:2017-02-05 14:51:13
【问题描述】:

我正在尝试制作一个控制器方法:

public String CreateGasolineBlend(List<Guid> enumerableTransferIDs)
    {
        //Details
    }

接受 GUID 列表。但是,当我使用邮递员时,列表始终为空。

我尝试使用这篇文章来了解如何格式化请求:

Is it possible to send an array with the Postman Chrome extension?

但我不确定 MVC 是否能够使用 Post 方法接受对象列表,或者我是否在 Postman 中错误地格式化了 POST 请求。

(我使用的是前面的堆栈溢出问题状态,arr[0]、arr[1] 或 arr[]、arr[] 和 Guids 作为值。)

是我在控制器中接收值的方式有问题,还是我使用 Postman 的方式有问题?

【问题讨论】:

    标签: c# arrays asp.net-mvc postman


    【解决方案1】:

    MVC 能够接受列表对象并将其映射到操作方法参数。

    您需要确保在发送数据时使用正确的Content-Type 标头值。 "application/json" 应该可以工作。

    我刚刚更新了您的操作方法以将发布的数据与项目计数一起返回到 json 结构中以验证这一点。

    [HttpPost]
    public ActionResult CreateGasolineBlend(List<Guid> enumerableTransferIDs)
    {
        return Json(new { ItemCount= enumerableTransferIDs.Count(),
                          Items= enumerableTransferIDs});
    }
    

    您可以看到响应与我们发布的数据一起返回。

    【讨论】:

    • 这完全解决了我的问题。我对 MVC 和 Web 开发还很陌生,所以我不知道发送表单数据与原始/应用程序/json 之间存在差异。感谢您为我指明正确的方向。
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多