WCF REST JSON请求:格式化程序异常

时间:2018-12-06 08:37:09

标签: json rest

我的Rest API中有一个枚举作为参数之一。

端点合同:

List<Transaction> GetTransactions(int employeeID, int recordOffset, int recordLimit, TransactionType transactionType = TransactionType.All);

InputJSON请求:

{
            "employeeID":"123",
            "recordOffset": 0,
            "recordLimit": 80,
            "transactionType":"All"
}

当我使用transactionType键传递此输入JSON时,我收到了400个错误的请求。

请让我知道如何,我应该在json请求中传递一个ENUM值。

public enum TransactionType
{
    All = 0,
    Incoming = 1,
    Outgoing = 2
}

异常消息:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://Services/2015/03:transactionType. The InnerException message was 'There was an error deserializing the object of type Entities.TransactionType. The value 'All' cannot be parsed as the type 'Int64'.'.  Please see InnerException for more details

1 个答案:

答案 0 :(得分:1)

期望的参数是>>> value = 2 >>> f"{value:+g}" '+2' >>> value = -2.00001 >>> f"{value:+g}" '-2.00001' >>> value = 2.00001 >>> f"{value:+g}" '+2.00001' 。 您应该在此处的JSON请求中传递 List<string> listmobilenumber = new List<string>(); string filePath = string.Empty; if (attachmentcsv != null) { string path = Server.MapPath("~/Uploads/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } filePath = path + Path.GetFileName(attachmentcsv.FileName); string extension = Path.GetExtension(attachmentcsv.FileName); attachmentcsv.SaveAs(filePath); string csvData = System.IO.File.ReadAllText(filePath); foreach (string row in csvData.Split('\r','\n')) { if (!string.IsNullOrEmpty(row)) { listmobilenumber.Add(row); } } } int0的int值。

根据您构建JSON请求的方式,可以添加所需的Enum的值,例如1

这样,您可以在建立请求时实际使用Enum来确保使用可行的值。