Alma
Alma

Reputation: 4390

what variable type I can use for response back from Web API

I call a web Api that is returning me back a response. at first I define the response as var but not I return to define the response at the top of the method. Ia m not sure what type of variable I have to use for it. I used dictionary but I am getting this is in new keyword.

cannot implicitly convert type 'AnonymousType#1' to Dictionary.

This is my code:

    [AllowAnonymous]
    [Route("searchuser")]
    [HttpPost]
    public async Task<ActionResult> SearchUser(UserInfo userInfo)
    {
          object userObject = null;

          Dictionary<string, object> respone = new Dictionary<string, object>();

          if (userInfo.LastName != null && userInfo.Zip != null && userInfo.Ssn != null)
            {

                UserKey userKey = new UserKey();
                userKey.AccountKey = accessKey;
                var response = await httpClient.PostAsJsonAsync(string.Format("{0}{1}", LoanApiBaseUrlValue, "/verifyuser"), userKey);
                if (response.IsSuccessStatusCode)
                {
                    userObject = new JavaScriptSerializer().DeserializeObject(response.Content.ReadAsStringAsync().Result) as object;
                    var json = response.Content.ReadAsStringAsync().Result;
                    var userVerify = new JavaScriptSerializer().Deserialize<VerifyUser>(json);
                }
            }

             respone = new   // this is where I am getting the error before I was using var and it was working.
            {
                success = userObject != null,
                data = userObject
            };
            return Json(respone, JsonRequestBehavior.AllowGet);
        }

Upvotes: 1

Views: 167

Answers (1)

Pedro Fernandes Filho
Pedro Fernandes Filho

Reputation: 539

Sending a result in JSON you can use any type of variable (List, Dictionary, int, string, ...).

You can do something like this:

public async Task<HttpResponseMessage> SearchUser(UserInfo userInfo) {
    HttpResponseMessage response = new HttpResponseMessage();

    try
    {
        object userObject = null;

        // Do something

        object result = new { success = userObject != null, data = userObject };

        response = Request.CreateResponse(HttpStatusCode.OK, result);
    }
    catch (Exception e)
    {
        // Do Log

        response = Request.CreateResponse(HttpStatusCode.BadRequest, e.Message);
    }

    var tc = new TaskCompletionSource<HttpResponseMessage>();
    tc.SetResult(response);
    return tc.Task;
}

And you'll have your return in JSON, regardless of the type of the variable, which in this case is an anonymous object type.

The error message "cannot implicitly convert type 'AnonymousType#1' to Dictionary." is because you are trying to set a value of anonymous object type to a variable of type Dictionary:

// ...
Dictionary<string, object> respone = new Dictionary<string, object>();
// ...
respone = new   // this is where I am getting the error before I was using var and it was working.
{
    success = userObject != null,
    data = userObject
};

Upvotes: 1

Related Questions