首页 > 解决方案 > 如何通过邮递员将自定义对象数组作为多格式数据内容发送到asp.net web api?

问题描述

如何通过 Postman 将自定义对象数组正确发送到 asp.net web api?我所做的是:

自定义类:

public class SystemConsumerConfiguration
    {
        public int SystemId { get; }
        public Uri CallbackUrl { get; }

        public SystemConsumerConfiguration()
        {
        }

        public SystemConsumerConfiguration(int systemId, Uri callbackUrl)
        {
            SystemId = systemId;
            CallbackUrl = callbackUrl;
        }
   }

在 REST API 中查看模型:

public class PostDigitalPostSubscriptionConfiguration
    {
        public IReadOnlyList<SystemConsumerConfiguration> SystemsModel { get; set; }

        public IFormFile Certificate { get; set; }


        public PostDigitalPostSubscriptionConfiguration()
        {
        }
    }

现在,我在 Postman 中提出请求

邮递员请求

问题是,该模型与默认值绑定: 结果

标签: asp.net-web-apipostmanmultipartform-data

解决方案


忘记在SystemConsumerConfiguration.

应该是这样的:

public class SystemConsumerConfiguration
    {
        public int SystemId { get; set; }
        public Uri CallbackUrl { get; set; }

        public SystemConsumerConfiguration()
        {
        }

        public SystemConsumerConfiguration(int systemId, Uri callbackUrl)
        {
            SystemId = systemId;
            CallbackUrl = callbackUrl;
        }
   }

答案在: asp.net mvc 视图模型中的默认值


推荐阅读