Skip to content

Commit fcccc2b

Browse files
committed
修复ModelBinder的奇妙BUG
1 parent 9f31171 commit fcccc2b

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

Routine/Routine.APi/Controllers/CompanyCollectionsController.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public CompanyCollectionsController(IMapper mapper, ICompanyRepository companyRe
2525
throw new ArgumentNullException(nameof(companyRepository));
2626
}
2727

28-
[HttpGet("({ids})",Name =nameof(GetCompanyCollection))]
28+
[HttpGet("({ids})", Name = nameof(GetCompanyCollection))]
2929
public async Task<IActionResult> GetCompanyCollection([FromRoute]
3030
[ModelBinder(BinderType = typeof(ArrayModelBinder))]
3131
IEnumerable<Guid> ids)
@@ -36,11 +36,10 @@ public async Task<IActionResult> GetCompanyCollection([FromRoute]
3636
}
3737
var entities = await _companyRepository.GetCompaniesAsync(ids);
3838

39-
//这个写法有 BUG,ids.Count() 返回的居然是 Query 字符串长度
40-
//if (ids.Count() != entities.Count())
41-
//{
42-
// return NotFound();
43-
//}
39+
if (ids.Count() != entities.Count())
40+
{
41+
return NotFound();
42+
}
4443

4544
var dtosToReturn = _mapper.Map<IEnumerable<CompanyDto>>(entities);
4645
return Ok(dtosToReturn);

Routine/Routine.APi/Helpers/ArrayModelBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
3333
var converter = TypeDescriptor.GetConverter(elementType);
3434
var values = value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
3535
.Select(x => converter.ConvertFromString(x.Trim())).ToArray();
36-
var typedValues = Array.CreateInstance(elementType, value.Length);
36+
var typedValues = Array.CreateInstance(elementType, values.Length);
3737
values.CopyTo(typedValues, 0);
3838
bindingContext.Model = typedValues;
3939
bindingContext.Result = ModelBindingResult.Success(bindingContext.Model);

0 commit comments

Comments
 (0)