3

I want to send response message like this

{"message_code": 00, "message": "Success"}

My Response Message Class

 public class ResponseMessage
 {
    public string MessageCode { get; set; }
    public string Message { get; set; }
 }

And my Message Method

public ResponseMessage Message(FundTransfer transfer)
{
    var checkBalance = 
    BusinessLayer.Api.AccountHolderApi.GetBalance(transfer.FromAccount);

    if (checkBalance.Balance.ToDecimal() < transfer.Amount)
    {
        return new ResponseMessage
        {
            MessageCode = "22",
            Message = "Insufficient Balance"
        };
    }
    else
    {
        var transferData = BusinessLayer.Api.AccountHolderApi
            .FundTransfer(transfer);

        if (transferData == true)
        {
            return new ResponseMessage { MessageCode = "00", Message = "Success" };
        }
        else
        {
            return new ResponseMessage { MessageCode = "11", Message = "Some Error" };
        }
    }  
}

And my API Method

[HttpPost]
[ActionName("transfer")]
public IHttpActionResult FundTransfer([FromBody] FundTransfer transfer)
{
    var response = Message(transfer);
    if (response.MessageCode == "11")
    {
        return Content(HttpStatusCode.BadRequest, response.Message);
    }
    else
    {
        return Ok(response.Message);
    }
}

Now for example when the transferData == true in my Message() Method I get only message object value as "Success" since in my return content I am returning the response.Message. I want to get the response in JSON as

{"message_code": 00, "message": "Success"}

instead of just "success"

Currently learning Web API.

1 Answer 1

3

Refactor API Method to return model instead of just the message property

[HttpPost]
[ActionName("transfer")]
public IHttpActionResult FundTransfer([FromBody] FundTransfer transfer) {
    var response = Message(transfer);
    if (response.MessageCode == "11") {
        return Content(HttpStatusCode.BadRequest, response);
    } else {
        return Ok(response);
    }
}

Also to get message like this

{"message_code": "00", "message": "Success"}

Refactor ResponseMessage class via JSON property attribute...

 public class ResponseMessage {
    [JsonProperty("message_code")]
    public string MessageCode { get; set; }
    [JsonProperty("message")]
    public string Message { get; set; }
 }

Or construct model anonymously

[HttpPost]
[ActionName("transfer")]
public IHttpActionResult FundTransfer([FromBody] FundTransfer transfer) {
    var response = Message(transfer);
    var model = new {
        message_code = response.MessageCode,
        message = response.Message
    };
    if (response.MessageCode == "11") {
        return Content(HttpStatusCode.BadRequest, model);
    } else {
        return Ok(model);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.