从邮递员调用时,以json发送的对象数据未在对象模型中序列化

时间:2019-06-11 04:25:12

标签: asp.net-web-api postman

我使用以下代码创建了一个Web API。

[HttpPost]
[Route("api/Agents/SetAgentSettings")]
public HttpResponseMessage SetAgentSetting(string agentIp, string agentMac, Guid orgId,SettingModel settingData)
{
}

我想从邮递员使用以下请求调用此api。

http://localhost:50194/api/Agents/SetAgentSettings?agentIp=10.0.1.33&agentMac=E442A6273481&orgId=C1F62D47-FBDF-468E-A4E6-418BFD8EB525

在正文中,我将发送以下正文:

{
    "agentIp":"10.0.2.10",
    "agentMac":"Computer1",
    "orgId":"c1f62d47-fbdf-468e-a4e6-418bfd8eb525",
    "settingData":"{\"IsAutoSyncActive\":false,\"AutoSyncInterval\":0,\"AutosyncTime\":\"00:00\",\"IsRealtimeSyncActive\":false,\"RealTimeSyncUrl\":null,\"LogTargets\":6,\"LogSeveritys\":15,\"Exports\":0,\"LogInDetail\":true,\"LogInDatabase\":true,\"NotifyEmailId\":null,\"DiagonisticsMode\":false,\"ResyncRule\":null,\"ResyncBatchCount\":\"10\",\"IsResyncScheduled\":false,\"ExecuteFor\":1,\"Batch\":0,\"SaveSyncInfoToDb\":false,\"RealTimePort\":null,\"NotificationRule\":null,\"IsNotificationMailEnabled\":false,\"FileDeleteTime\":null,\"AgentType\":null,\"Frequency\":\"DAILY\",\"PartitionKey\":\"c1f62d47-fbdf-468e-a4e6-418bfd8eb525\",\"RowKey\":\"fbc6b368-9251-4165-a36b-fc1bd3912925\",\"Timestamp\":\"0001-01-01T00:00:00+00:00\",\"ETag\":null}"

}

在控制器中,我获取所有数据,但设置模型未序列化。如何发送设置模型数据。

1 个答案:

答案 0 :(得分:0)

您正在以JSON字符串传递settingData,而另一端希望将其与模型绑定,但无论如何将无法正常工作。您必须以JSON格式传递模型。另外,当您在URL中传递agentIpagentMacorgId时,无需再次传递正文。

在此假设您的SettingModel如下,

public class SettingModel
{
    public bool IsAutoSyncActive { get; set; }
    public int AutoSyncInterval { get; set; }
    public string AutosyncTime { get; set; }
    public bool IsRealtimeSyncActive { get; set; }
    public object RealTimeSyncUrl { get; set; }
    public int LogTargets { get; set; }
    public int LogSeveritys { get; set; }
    public int Exports { get; set; }
    public bool LogInDetail { get; set; }
    public bool LogInDatabase { get; set; }
    public object NotifyEmailId { get; set; }
    public bool DiagonisticsMode { get; set; }
    public object ResyncRule { get; set; }
    public string ResyncBatchCount { get; set; }
    public bool IsResyncScheduled { get; set; }
    public int ExecuteFor { get; set; }
    public int Batch { get; set; }
    public bool SaveSyncInfoToDb { get; set; }
    public object RealTimePort { get; set; }
    public object NotificationRule { get; set; }
    public bool IsNotificationMailEnabled { get; set; }
    public object FileDeleteTime { get; set; }
    public object AgentType { get; set; }
    public string Frequency { get; set; }
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Timestamp { get; set; }
    public object ETag { get; set; }
}

您的请求应为:

URL:http://localhost:50194/api/Agents/SetAgentSettings?agentIp=10.0.1.33&agentMac=E442A6273481&orgId=C1F62D47-FBDF-468E-A4E6-418BFD8EB525

身体:

{
    "IsAutoSyncActive": false,
    "AutoSyncInterval": 0,
    "AutosyncTime": "00:00",
    "IsRealtimeSyncActive": false,
    "RealTimeSyncUrl": null,
    "LogTargets": 6,
    "LogSeveritys": 15,
    "Exports": 0,
    "LogInDetail": true,
    "LogInDatabase": true,
    "NotifyEmailId": null,
    "DiagonisticsMode": false,
    "ResyncRule": null,
    "ResyncBatchCount": "10 ",
    "IsResyncScheduled": false,
    "ExecuteFor": 1,
    "Batch": 0,
    "SaveSyncInfoToDb": false,
    "RealTimePort": null,
    "NotificationRule": null,
    "IsNotificationMailEnabled": false,
    "FileDeleteTime": null,
    "AgentType": null,
    "Frequency": "DAILY ",
    "PartitionKey": "c1f62d47-fbdf-468e-a4e6-418bfd8eb525",
    "RowKey": "fbc6b368-9251-4165-a36b-fc1bd3912925",
    "Timestamp": "0001-01-01T00:00:00+00:00",
    "ETag": null
}

方法:

[HttpPost]
[Route("api/Agents/SetAgentSettings")]
public void SetAgentSetting(string agentIp, string agentMac, Guid orgId, [FromBody]SettingModel settingData)
{
}