Developer Guide
JSON Response

    Using this response type you can serialize objects into JSON (JavaScript Object Notation) and pass them to the requester. When using this response type, be sure to decorate your action with the "HandleExceptionsForJson" attribute. Using this annotation will make the Magentrix platform ensure that all errors and exceptions are transmitted back to the consumer in a JSON friendly format.
     

    Note: Magentrix offers a full set of RESTful APIs for most use cases. Check out the Magentrix REST API for more information.

     

    Methods

    PropertyDescription
    Json(object model)HTTP POST only model serialization
    Json(object model, int statusCode)HTTP POST with custom StatusCode
    Json(object model, int statusCode, JsonRequestBehavior behavior)GET or POST with custom StatusCode
    Json(object model, string contentType)HTTP POST with custom contentType
    Json(object model, string contentType, Encoding contentEncoding)HTTP POST with custom contentType and Encoding
    Json(object model, JsonRequestBehavior behavior)GET or POST based on behavior parameter.
    Json(object model, bool allowGet)Easier to use overload

     

    The example below demonstrates how you can retrieve a record from database and pass it as JSON string to the consumer:
     
    [HandleExceptionsForJson]
    public ActionResponse GetContact(string id)
    {
        Contact con = Database.Retrieve<Contact>(id);
        return Json(con, JsonRequestBehavior.AllowGet);
    }

    If you don't use the "HttpPost" annotation on your action, then by default your Json action will be a served for GET requests.

    Since in most cases it is recommended to use POST for JSON requests for security reasons, in order to respond to GET requests you can provide a implicit permission to Magentrix. In order to execute the above action, if you assume that your ActiveController name is "DbcontextController" then the custom url will be:

    curl: /dbcontext/getContact?id=003R000000haXk3IAE

     

    Example of writing a HTTP POST JSON action:

    [HttpPost]
    [HandleExceptionsForJson]
    public ActionResponse Contact(string id)
    {
        Contact con = Database.Retrieve<Contact>(id);
        return Json(con, JsonRequestBehavior.AllowGet);
    }