Мы возвращаем ошибку немедленно, вызывая исключение 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 errors = new List();
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(errors, HttpStatusCode.BadRequest);
throw new HttpResponseException(responseMessage);
}
Подробнее здесь: https://stackoverflow.com/questions/107 ... et-web-api
Мобильная версия