我对我们向客户返回错误的方式感到担忧。当我们收到错误消息时,是否通过抛出HttpResponseException立即返回错误消息:public void Post(Customer customer){ if (string.IsNullOrEmpty(customer.Name)) { throw new HttpResponseException("Customer Name cannot be empty", HttpStatusCode.BadRequest) } if (customer.Accounts.Count == 0) { throw new HttpResponseException("Customer does not have any account", HttpStatusCode.BadRequest) }}否则我们会累积所有错误,然后发回给客户:public void Post(Customer customer){ List<string> errors = new List<string>(); if (string.IsNullOrEmpty(customer.Name)) { errors.Add("Customer Name cannot be empty"); } if (customer.Accounts.Count == 0) { errors.Add("Customer does not have any account"); } var responseMessage = new HttpResponseMessage<List<string>>(errors, HttpStatusCode.BadRequest); throw new HttpResponseException(responseMessage);}这只是一个示例代码,与验证错误或服务器错误无关,我只想了解最佳实践,每种方法的优缺点。
3 回答
- 3 回答
- 0 关注
- 647 浏览
添加回答
举报
0/150
提交
取消