Skip to content
Diogo Castro edited this page May 13, 2016 · 6 revisions

List of helper methods

200 OK

JSendOk()
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
    "status" : "success",
    "data" : null
}
JSendOk(article)
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
    "status" : "success",
    "data" : {
        "title" : ""
    }
}

201 Created

JSendCreated("http://localhost/articles/5", article)
JSendCreatedAtRoute("GetArticle", new {id = 5}, article)
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Location: http://localhost/articles/5

{
    "status" : "success",
    "data" : {
        "title" : ""
    }
}

302 Found

JSendRedirect("http://localhost/articles/5")
JSendRedirectToRoute("GetArticle", new {id = 5})
HTTP/1.1 302 Found
Content-Type: application/json; charset=utf-8
Location: http://localhost/articles/5

{
    "status" : "success",
    "data" : null
}

400 Bad Request

JSendBadRequest(reason)
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
    "status" : "fail",
    "data" : "reason"
}
JSendBadRequest(ModelState)
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
    "status" : "fail",
    "data" : { 
        "article.Title" : "The Title field is required."
    }
}

401 Unauthorized

JSendUnauthorized(new AuthenticationHeaderValue("challenge-scheme"))
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
WWW-Authenticate: challenge-scheme

{
    "status" : "fail",
    "data" : "Authorization has been denied for this request."
}

404 Not Found

JSendNotFound()
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8

{
    "status" : "fail",
    "data" : "The requested resource could not be found."
}
JSendNotFound(reason)
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8

{
    "status" : "fail",
    "data" : "reason"
}

500 Internal Server Error

JSendInternalServerError(string message, int? errorCode = null, object data = null)
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8

{
    "status" : "error",
    "message" : "the server failed because of reasons",
    "code": 92,
    "data" : "placeholder for additional data"
}
JSendInternalServerError(Exception ex, string message = null, int? errorCode = null, object data = null)
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8

{
    "status" : "error",
    "message" : "custom message or exception message if none is provided",
    "code": 92,
    "data" : "additional data or exception's stack trace if none is provided"
}

Misc

  • JSend<TResponse>(HttpStatusCode, TResponse) where TResponse : IJSendResponse - creates an arbitrary JSend response
  • JSendSuccess(HttpStatusCode, object) - creates an arbitrary "success" response
  • JSendFail(HttpStatusCode, object) - creates an arbitrary "fail" response
  • JSendError(HttpStatusCode, string, int, object) - creates an arbitrary "error" response