API 不接受对象列表作为参数 .Net Core webAPI [英] API not accepting List of objects as a parameter .Net Core webAPI

查看:31
本文介绍了API 不接受对象列表作为参数 .Net Core webAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个接受对象列表作为参数的 API.这是我正在使用的 Api:

[HttpPost(PharmacySupply")]公共异步任务<List<PharmacyMedicineSupply>>GetPharmacySupply(List medDemand){SupplyRepo sr = 新的 SupplyRepo();返回等待 sr.GetPharmacySupply(medDemand);}

这是我的模型:

公共类 MedicineDemand{公共字符串医学{得到;放;}公共 int DemandCount { 得到;放;}}

我在 swagger 中将此作为参数传递:

<预><代码>[{多洛,7},{胆钙化醇,9},{Orthoherb,5},{Gaviscon,2},{海拉克,3}{Cyclopam,1}]

但参数为空.调试时显示计数为 0.我做错了什么?

解决方案

如果您正在使用邮递员进行测试,请尝试添加 [FromBody].(参见 [FromBody] 文档这里)

 public async Task>GetPharmacySupply([FromBody] List medDemand)

要在邮递员中进行测试,您必须使用

[{ 医药":Dolo"、DemandCount";: 7},{药物":胆钙化醇",DemandCount";: 9},...]

如果你尝试用这个来大摇大摆地测试

[{多洛", 7 },{胆钙化醇", 9 },...]

I am working on an API that accepts a list of Objects as a parameter. This is the Api I am working with:

[HttpPost("PharmacySupply")]
        public async Task<List<PharmacyMedicineSupply>> GetPharmacySupply(List<MedicineDemand> medDemand)
        {
            SupplyRepo sr = new SupplyRepo();
            return await sr.GetPharmacySupply(medDemand);
        }

This is my model :

public class MedicineDemand
    {
        public string Medicine { get; set; }
        public int DemandCount { get; set; }
    }

I am passing this in swagger as parameter:

[
{Dolo,7},
{Cholecalciferol,9},
{Orthoherb,5},
{Gaviscon,2},
{Hilact,3}
{Cyclopam,1}
]

but the parameter is empty . It is showing a count of 0 while debugging. What am I doing wrong?

解决方案

Try to add [FromBody] if you are testing using postman. (See [FromBody] documentation here)

 public async Task<List<PharmacyMedicineSupply>> GetPharmacySupply([FromBody] List<MedicineDemand> medDemand)

for test in postman you have to use

[
   { "Medicine": "Dolo", "DemandCount" : 7},
   { "Medicine": "Cholecalciferol", "DemandCount" : 9},
   ...
]

and if maybe you try for testing in swagger with this

[
   { "Dolo" , 7 },
   { "Cholecalciferol" , 9 },
   ...
]

这篇关于API 不接受对象列表作为参数 .Net Core webAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆