0
在許多代碼重複操作的基礎上驗證屬性(基於模型狀態)時,我必須拋出自定義異常。在驗證特定操作參數時拋出自定義異常
這怎麼可以重構?
當前代碼示例
[HttpGet("{jobId}/employeeLog")]
public async Task<IActionResult> GetFileLog(Guid jobId,
[FromQuery] Guid employeeId,
[SkipAndTopFromQuery] int? skip,
[SkipAndTopFromQuery] int? top,
[FromHeader(Name = "X-Correlation-ID")] string correlationId)
{
var employeeIdToValidate = ModelState["employeeId"];
var jobIdToValidate = ModelState["jobId"];
if (employeeIdToValidate == null || employeeIdToValidate != null && employeeIdToValidate.Errors.Any())
throw new InvalidFileIdException();
if (jobIdToValidate == null || jobIdToValidate != null && jobIdToValidate.Errors.Any())
throw new InvalidApplicationIdException();
if (!ModelState.IsValid)
return StatusCode((int)HttpStatusCode.BadRequest);
....
}