The MVC
already makes the automatic validation of the Model by Voce. To get the errors of your Voce model you can access the property ModelState
. Example:
var errors = string.join(" ", ModelState.SelectMany(s => s.Value.Errors)
.Select(e => e.ErrorMessage))
if(errors.Length > 0){
return BadRequest(errors);
}
To display friendly messages for the Voce user you must set the property ErrorMessage
in your validation attributes, in your model. Example:
public class Modelo {
[StringLength(1, ErrorMessage = "Introduza um nome válido")]
public string Name{get;set;}
}
If this is not enough for you. You can always validate the model programmatically to know what the errors are. Example:
var context = new ValidationContext(model, null, null);
var results = new List<ValidationResult>();
bool valid = Validator.TryValidateObject(model, context, results, true);
if (valid)
{
return Task.CompletedTask;
}
var errors = string.Join(" ", results.Select(e => e.ErrorMessage));
throw new InvalidOperationException($"Can not insert because data is not valid. {errors}");
What I understand as "bad request" is code in an HTTP header of the response. And in this case the response would not even contain much more information. But this you could not spit several times into a foreach, only one, and then send the reply to the customer.
– bfavaretto
I usually return
200
and aJSON
orXML
with the additional information.– Roberto de Campos
@Robertodecampos I’m sorry to say but to return saying that it’s "okay" is complicated. If you are responsible for the server and the client, then you can manage that very well. Otherwise, many applications/programmers assume that code 200 is successful. Use 4XX range error for request errors (client didn’t know what he wanted/client caused the error) and 5XX for server-side errors.
– Jefferson Quesado
@pnet, a BAD REQUEST itself is just an HTTP code, if I’m not mistaken it’s 400. On some REST systems simply reporting this is enough. For example, since I only need to talk to client applications that are machines (without interacting with people), I just return the error code. But it is not your case, for you it is interesting to return also the message/ the error messages. I would risk a response in Java, where I can return an arbitrary string and an arbitrary HTTP code. But I don’t know C# at this point
– Jefferson Quesado
The question is how to do this. There is a service that validates whether or not the guy filled a field. This field is required
– pnet
Does anyone know of a link that explains how to send a bad request through a Rest? What should I do, or rather, how
– pnet
How do I return a code 400, for example? How do I do this?
– pnet
Are you using web.api? If you have your action you can return a Badrequest() as long as your return is Iactionresult.
– Gabriel Coletta
@Gabrielcoletta, that’s right. Just ask me a question. I need to inform in this Badrequest products that are without serial. I mount an object with these values and put it in the parameter of the badRequest type: Return Badrequest(meu_object); that’s right?
– pnet
Badrequest does not accept an object, just string. If you use Stringbuilder and mount the message it would work.
– Gabriel Coletta
@bfavaretto O rfc7231 disagree.
the server SHOULD send a representation containing an explanation of the error situation
. But yes the answer is only one (there are not several bad requests). It could also be oneMulti-Status
– Bruno Costa