Skip to content

A small framework for turning exceptions into good Rest responses in WebApi

Notifications You must be signed in to change notification settings

razer21/WebApiProblem

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WebApiProblem

A small framework for turning exceptions into good Rest responses in WebApi

It is based on the recomendations in http://tools.ietf.org/html/draft-nottingham-http-problem-03

Use

  1. Install the package from nuget:

    PM > Install-Package WebApiProblem
    
  2. Register the ResponseExceptionFilterAttribute (either in ApplicationStart or wherever you register your filters)

    GlobalConfiguration.Configuration.Filters.Add(new WebApiProblem.ResponseExceptionFilterAttribute());
    
  3. Throw an exception of type BasicApiProblemException

     // GET api/values/5
    public string Get(int id)
    {
         throw new BasicApiProblemException(HttpStatusCode.BadRequest, 
    		"That was a bad request", "http://www.api-problems.com/really-bad-request");
    }
    
  4. View the well understood response and be happy

    400 - Bad request
    {
    	"problemType": "http://www.api-problems.com/really-bad-request",
    	"title": "That was a bad request",
    	"detail": "Please send me BETTER requests in the future"
    }
    

More elaborate problem messages

To create more elaborate problem message, inherit from ApiProblemException and define a serializable model that inherits from ApiProblem for T.

About

A small framework for turning exceptions into good Rest responses in WebApi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published