Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like this, for example:</p> <pre><code>public HttpResponseMessage Post(Person person) { if (ModelState.IsValid) { PersonDB.Add(person); return Request.CreateResponse(HttpStatusCode.Created, person); } else { // the code below should probably be refactored into a GetModelErrors // method on your BaseApiController or something like that var errors = new List&lt;string&gt;(); foreach (var state in ModelState) { foreach (var error in state.Value.Errors) { errors.Add(error.ErrorMessage); } } return Request.CreateResponse(HttpStatusCode.Forbidden, errors); } } </code></pre> <p>This will return a response like this (assuming JSON, but same basic principle for XML):</p> <pre><code>HTTP/1.1 400 Bad Request Content-Type: application/json; charset=utf-8 (some headers removed here) ["A value is required.","The field First is required.","Some custom errorm essage."] </code></pre> <p>You can of course construct your error object/list any way you like, for example adding field names, field id's etc.</p> <p>Even if it's a "one way" Ajax call like a POST of a new entity, you should still return something to the caller - something that indicates whether or not the request was successful. Imagine a site where your user will add some info about themselves via an AJAX POST request. What if the information they have tried to entered isn't valid - how will they know if their Save action was successful or not?</p> <p>The best way to do this is using <strong>Good Old HTTP Status Codes</strong> like <code>200 OK</code> and so on. That way your JavaScript can properly handle failures using the correct callbacks (error, success etc).</p> <p>Here's a nice tutorial on a more advanced version of this method, using an ActionFilter and jQuery: <a href="http://asp.net/web-api/videos/getting-started/custom-validation">http://asp.net/web-api/videos/getting-started/custom-validation</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload