Post 使用 HttpClient 不绑定模型
Post with HttpClient doesn't bind model
从 azure 函数到 API
的简单 post
using (var response = await httpClient.PostAsJsonAsync(installationServiceUrl, deviceInstallation.ToRequestBody()))
{...}
API 收到请求,但无法从请求中绑定模型
但是 Request.Content
不为空并且包含已发送 JSON
object。 Content-Type header 设置为 application/json.
有什么建议吗?
更新:据我了解,API 认为 Model 是简单的字符串值 (locationId
),至少我从 ModelState.Keys
[=] 中是这样理解的41=]。它只包含 locationId
.
更新:ToRequestBody
方法只是改变 object
的形状
public static DeviceInstallationRequest ToRequestBody(this DeviceInstallation deviceInstallation)
{
return new DeviceInstallationRequest()
{
InstallationId = deviceInstallation.InstallationId,
Name = deviceInstallation.Name,
StartDateTime = deviceInstallation.StartDateTime,
EndDateTime = deviceInstallation.EndDateTime,
CreatedDateTime = deviceInstallation.CreatedDateTime,
InstallationType = deviceInstallation.InstallationType,
Production = deviceInstallation.Production,
Default = deviceInstallation.Default
}
}
API 侧的预期模型:
public class BindDeviceInstallationRequest
{
[Required]
public string InstallationId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public DateTime StartDateTime { get; set; }
[Required]
public DateTime EndDateTime { get; set; }
[Required]
public DateTime CreatedDateTime { get; set; }
[Required]
public InstallationType InstallationType { get; set; }
[Required]
public bool Production { get; set; }
[Required]
public bool Default { get; set; }
}
如果是编码问题,请尝试自己构建内容并将其发送到服务器,
DeviceInstallationRequest model = deviceInstallation.ToRequestBody();
string json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, Encoding.UTF8, "application/json");
using (var response = await httpClient.PostAsync(installationServiceUrl, content)) {
//...
}
这样您就可以完全控制发送到服务器的内容。
调试时,检查来自客户端的原始 JSON 以及服务器上收到的内容。
从 azure 函数到 API
的简单 postusing (var response = await httpClient.PostAsJsonAsync(installationServiceUrl, deviceInstallation.ToRequestBody()))
{...}
API 收到请求,但无法从请求中绑定模型
但是 Request.Content
不为空并且包含已发送 JSON
object。 Content-Type header 设置为 application/json.
有什么建议吗?
更新:据我了解,API 认为 Model 是简单的字符串值 (locationId
),至少我从 ModelState.Keys
[=] 中是这样理解的41=]。它只包含 locationId
.
更新:ToRequestBody
方法只是改变 object
public static DeviceInstallationRequest ToRequestBody(this DeviceInstallation deviceInstallation)
{
return new DeviceInstallationRequest()
{
InstallationId = deviceInstallation.InstallationId,
Name = deviceInstallation.Name,
StartDateTime = deviceInstallation.StartDateTime,
EndDateTime = deviceInstallation.EndDateTime,
CreatedDateTime = deviceInstallation.CreatedDateTime,
InstallationType = deviceInstallation.InstallationType,
Production = deviceInstallation.Production,
Default = deviceInstallation.Default
}
}
API 侧的预期模型:
public class BindDeviceInstallationRequest
{
[Required]
public string InstallationId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public DateTime StartDateTime { get; set; }
[Required]
public DateTime EndDateTime { get; set; }
[Required]
public DateTime CreatedDateTime { get; set; }
[Required]
public InstallationType InstallationType { get; set; }
[Required]
public bool Production { get; set; }
[Required]
public bool Default { get; set; }
}
如果是编码问题,请尝试自己构建内容并将其发送到服务器,
DeviceInstallationRequest model = deviceInstallation.ToRequestBody();
string json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, Encoding.UTF8, "application/json");
using (var response = await httpClient.PostAsync(installationServiceUrl, content)) {
//...
}
这样您就可以完全控制发送到服务器的内容。
调试时,检查来自客户端的原始 JSON 以及服务器上收到的内容。