Simple implementation of the JSend specification for ASP.NET Web API
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
source
.gitattributes
.gitignore
LICENSE
README.md
WebApi.JSend.sln
appveyor.yml

README.md

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)
    { }
}