0

I'm trying to create a custom JSON Response message on C# Web API. Right now Web Api only returns the parameter inserted by the user on Web API. I want the Web API to returning the response code and respond message along with the parameter. I'm trying to create a JSON object with returning data and response message inside like this

JObject _jo = new JObject
 {
     { "Data", value },
     { "Response Code:", Constant.RESP_SUCCESS },
     { "Respnse Message", " Success"}                   
 }; 

but there is an exception is thrown

CS1503  Argument 2: cannot convert from 'His_sso_final.Services.TacVerify_Model' to 'Newtonsoft.Json.Linq.JToken'
        

And this is the actual response I get using the method HttpResponseMessage verifyAccessToken

Actual Response

[
    {
        "customer_id": "LCE500008_2021                                                                                                                                                                                                                                                 "
    }
]

Expected Response

{
    "customer _id": "LCE202101245002",
     "response": {
        "code": 00,
        "response_message": "Success"
    }

}

Method to call Web Api

public HttpResponseMessage verifyAccessToken(TacVerify_Model value)
        {
            try
            {
                TacVerify_Model _return = value;
                SSOService objCrd = new SSOService();
                List<TacVerify_Model> modelCust = objCrd.DoTacVerify(value);

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, modelCust);
                
                return response;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

  • i would suggest not to convert to JToken, just return the object like this return Request.CreateResponse(HttpStatusCode.OK, your response); – vivek nuna Feb 13 '21 at 08:11
  • `HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new { customer_id: modelCust.FirstOrDefault().customer_id, response: new { code: 00, response_message: "Success" } });` – Chetan Ranpariya Feb 13 '21 at 08:12
  • https://stackoverflow.com/questions/42360139/asp-net-core-return-json-with-status-code and just make an object that is shaped like the json you want – Caius Jard Feb 13 '21 at 08:17

0 Answers0