none
[Web API] How to return a object in HttpResponseMessage??? RRS feed

  • Question

  •   // GET api/Account/5
            [HttpGet]
            [ActionName("GetAccount")]
            public HttpResponseMessage GetAccount(int id)
            {
                Account value;
                try
                {
                    var myque = from x in db.Accounts where x.idUser==id select x;
                    value= myque.FirstOrDefault();
    
                }
                catch (Exception)
                {
                    return new HttpResponseMessage { Content = new StringContent("[{\"Success\":\"Fail\"},{\"Message\":\"Login Fail\"}]", System.Text.Encoding.UTF8, "application/json") };
                }
                return new HttpResponseMessage { Content = new StringContent("[{\"Success\":\"Success\"},{\"Message\":\"Login successfully\"}],{\"Data\":"+value+"}", System.Text.Encoding.UTF8, "application/json") };
            }
    

    I want to add value to code below

    return new HttpResponseMessage { Content = new StringContent("[{\"Success\":\"Success\"},{\"Message\":\"Login successfully\"}],{\"Data\":"+value+"}", System.Text.Encoding.UTF8, "application/json") };
    [
      {
        "Success": "Success"
      },
      {
        "Message": "Login successfully"
      }
      Value will display at here
    ]

    How to do that. I was trying to add to this StringContent then fail.

    Tuesday, November 29, 2016 5:37 AM