具有许多参数的.net核心API控制器操作

时间:2019-01-06 11:22:19

标签: api controller .net-core

我有一个需要30个参数的api操作。问题是当我在控制器类中添加此操作时遇到OverflowException。

[Route("api/getutilityservicebillcollection/{usba}/{usbt}/{usbpm}/{serviceUserID}" +
           "/{servicePassword}/{transactionSourceName}/{transactionSourceId}/{requestDateTime}" +
           "/{requestId}/{billNumber}/{billAccountNumber}/{billMobileNumber}/{billMonth}/{billYear}" +
           "/{billZone}/{billCategory}/{billStudentId}/{CustomerId}/{TransactionSerialNo}" +
           "/{BillToMonth}/{BillToYear}/{FromBillMonth}/{FromBillYear}/{studentBillPaymentType}" +
           "/{billPaymentAmount}/{paymentBranchId}/{paymentAccountNumber}/{exchangeCode}"+
           "/{lastPaymentDate}/{branchCode}")]
    public async Task<ActionResult> GetUtilityServiceBillCollection(string usba,
        string usbt, string usbpm, string serviceUserID,
        string servicePassword, string transactionSourceName, string transactionSourceId, string requestDateTime,
        string requestId, string billNumber, string billAccountNumber, string billMobileNumber, string billMonth,
        string billYear, string billZone, string billCategory, string billStudentId, string CustomerId,
        string TransactionSerialNo, string BillToMonth, string BillToYear, string FromBillMonth,
        string FromBillYear, string studentBillPaymentType, string billPaymentAmount, string paymentBranchId,
        string paymentAccountNumber, string exchangeCode, string lastPaymentDate, string branchCode)
    {
        UtilityServiceBillResponse s = await Iutility.UtilityServiceBillCollection(usba,usbt,usbpm,serviceUserID,
            servicePassword,transactionSourceName,transactionSourceId,requestDateTime,requestId,billNumber,
            billAccountNumber,billMobileNumber,billMonth,billYear,billZone,billCategory,billStudentId,CustomerId,
            TransactionSerialNo,BillToMonth,BillToYear,FromBillMonth,FromBillYear, studentBillPaymentType,
            billPaymentAmount, paymentBranchId, paymentAccountNumber, exchangeCode, lastPaymentDate,
            branchCode);

        if (s == null) return NotFound();
        else return Ok(s);

    }

这是什么问题?

我还要如何测试此操作?是否是在url上键入所有参数的唯一选择?

1 个答案:

答案 0 :(得分:0)

与其声明这样的API,不如使用属性来确保API的路由正确完成。 让我们添加一个新属性,以将其明确定义为POST请求

        [HttpPost]
        public ActionResult UtilityServiceBills([FromBody]UtilityServiceBillFilterViewModel vm)
        {
            // Do something to vm

            return Ok();
        }

这是ViewModel

public class UtilityServiceBillFilterViewModel
    {
        [Required]
        public string Usbt { get; set; }

        [Required]
        public long BillNumber { get; set; }

        public string transactionSourceName { get; set; }

    }

确保以这种方式发送有效载荷:

{
  "usbt": "asdasd",
  "transactionSourceName": "asdasdasdasd",
  "billNumber": 123123123
}

.NET Core足够聪明,可以将JSON对象转换为所述类。如果您想进一步了解基于[FromBody]的POST API与基于非[FromBody]的POST API之间的区别,请check this out