我有一个React Native(Expo运行在Android上的Expo Go上)客户端和一个ASP。NET Core Web API服务器。我的客户端发送内容类型为multipart/form-data
的请求,并为每个字段提供正确的值。但是当请求到达服务器时,请求(具有该字段可为空的模型)似乎丢失了字段的值并变为空,尽管发送了一个真实的值。
客户端(React Native移动的应用,使用axios
而不是fetch
):
// NationalId, Address, Phone, BirthDate are all strings. BirthDate is possibly undefined.
const formData = new FormData();
formData.append('NationalId', NationalId);
formData.append('Address', Address);
formData.append('Phone', Phone);
if (BirthDate) {
formData.append('BirthDate', BirthDate);
}
if (Image) {
formData.append('Image', Image);
}
const res = await axios.put(
`Employees/UpdateSelf/${NationalId}`,
formData,
{ headers: { 'Content-Type': 'multipart/form-data' } }
);
服务器:
public class UpdateSelfRequest
{
public string NationalId { get; set; } = string.Empty;
public string? Phone { get; set; }
public string? Address { get; set; }
public DateTimeOffset? BirthDate { get; set; }
public IFormFile? Image { get; set; }
}
[HttpPut("UpdateSelf/{NationalId}")]
[Authorize(Roles = AuthRoles.Employee)]
[Consumes("multipart/form-data")]
public async Task<IActionResult> UpdateSelf(
[FromRoute] string NationalId,
[FromForm] UpdateSelfRequest req)
{
// -----> Here, req.BirthDate is null despite the client sending a value
var result = await _service.UpdateSelf(NationalId, req);
if (!result.Success)
{
return Forbid();
}
return Ok();
}
一些日志:
Attempting to bind parameter 'req' of type 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest' ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexObjectModelBinder[44]
Attempting to bind parameter 'req' of type 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest' using the name '' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[13]
Attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.NationalId' of type 'System.String' using the name 'NationalId' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[14]
Done attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.NationalId' of type 'System.String'.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[13]
Attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.Phone' of type 'System.String' using the name 'Phone' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[14]
Done attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.Phone' of type 'System.String'.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[13]
Attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.Address' of type 'System.String' using the name 'Address' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[14]
Done attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.Address' of type 'System.String'.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[13]
Attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.BirthDate' of type 'System.Nullable`1[System.DateTimeOffset]' using the name 'BirthDate' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[15]
Could not find a value in the request with name 'BirthDate' for binding property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.BirthDate' of type 'System.Nullable`1[System.DateTimeOffset]'.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.SimpleTypeModelBinder[14]
Done attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.BirthDate' of type 'System.Nullable`1[System.DateTimeOffset]'.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[13]
Attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.Image' of type 'Microsoft.AspNetCore.Http.IFormFile' using the name 'Image' in request data ...
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[21]
No files found in the request to bind the model to.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[21]
No files found in the request to bind the model to.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormFileModelBinder[14]
Done attempting to bind property 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest.Image' of type 'Microsoft.AspNetCore.Http.IFormFile'.
dbug: Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexObjectModelBinder[45]
Done attempting to bind parameter 'req' of type 'Capstone.Features.EmployeeModule.Models.UpdateSelfRequest'.
1条答案
按热度按时间myzjeezk1#
解决(某种)
我很蠢,也许电视台的检查员骗了我。
我试图
append()
实际上是一个Date
对象,* 不是 * ISO时间戳字符串。我只是在TypeScript中使用了错误的显式类型(将BirthDate?: string
修复为BirthDate?: Date
,因为日期选择器组件将BirthDate
更改为Date
),因此它试图附加Date
,而网络检查器仍然显示ISO字符串,我的服务器根本无法绑定该属性。修复我的客户端上的类型起作用了,即使网络检查器仍然显示正在发送的相同数据。