Skip to content

Latest commit

 

History

History
51 lines (34 loc) · 1.82 KB

README.md

File metadata and controls

51 lines (34 loc) · 1.82 KB

WebApi.JSend

Build status NuGet Pre Release

Simple implementation of JSON responses in JSend format for ASP.NET Web API.

More information about the JSend specification.

Installing WebApi.JSend

You should install WebApi.JSend with NuGet:

Install-Package WebApi.JSend

This command from Package Manager Console will download and install WebApi.JSend and all required dependencies.

Using WebApi.JSend

The projects ships with JSendApiResponseActionFilter which wraps your response into a JSend JSON. This is all that's necessary to configure basic JSend responses:

var config = new HttpConfiguration();

config.Filters.Add(new JSendApiResponseActionFilter());

If you'd like to convert authorization errors to JSend format, add JSendAuthorizationAttribute either globally as shown below or on each controller/action like you would with regular AuthorizationAttribute.

config.Filters.Add(new JSendAuthorizationAttribute());

Finally, I found it handy to be able to throw exceptions from the service layer and have them convert to a JSend failure response. If you'd like this functionality simply add ApiExceptionToJSendApiResponseActionFilter to your filters.

config.Filters.Add(new ApiExceptionToJSendApiResponseActionFilter());

You can then create your custom exception types as shown below:

public class NotFoundException : ApiException
{
    public NotFoundException(string message) : base(HttpStatusCode.NotFound, message)
    { }
}