O'Reilly logo

Mastering ServiceStack by Andreas Niedermair

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Validating your requests

Supplementary to documentation, when implementing an API there should also be a validation included, as the most important mantra is: don't trust user input.

Whatever validation you will implement in your service, you should start off by implement the IHasResponseStatus interface on your response DTO:

using ServiceStack;

public class HelloResponse : IHasResponseStatus
{
  public string Result { get; set; }
  public ResponseStatus ResponseStatus { get; set; }
}

This will guarantee that any exception will get serialized into the ResponseStatus property. Additionally, the following exceptions will adapt the HTTP status code of the request:

  • ArgumentException and descendants returns 400 BadRequest
  • NotImplementedException returns 405 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required