asp.net-web-api - 从邮递员调用时,作为 json 发送的对象数据未在对象模型中序列化
问题描述
我使用下面的代码创建了一个 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}"
}
在控制器中,我得到了所有数据,但设置模型没有序列化。如何发送设置模型数据。
解决方案
您正在传递settingData
JSON 字符串,而另一方希望将其与模型绑定,这无论如何都行不通。您必须以 JSON 格式传递模型。此外,当您在 URL 中传递agentIp
, agentMac
,orgId
时,不需要再次传递正文。
在这里,我假设您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; }
}
您的要求应该是:
网址: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)
{
}