2

I am able to call the Web api method in client side and now i want make it in c# code. Here i am writing my jquery code.

$(document).ready(function () 
{
    $('#btnSubmit').click(function () 
    {
          var Params = 
          {
                    AsOndate: Todate,
                    BCRefCode: 100,
                    AccID: 90000
          };
          $.ajax({
                    type: "GET",
                    url: 'http://localhost:51093/api/account/',
                    //url: 'http://192.168.0.171:51093/api/account/',
                    data: Params,
                    dataType: "json",
                    traditional: true,
                    success: ajaxSuccess,
                    error: ajaxError
          });
});

and i am calling the web api method

public IEnumerable GetAccountListForMapping(Params param)
    {
        AccList _AccList = new AccList();
        ListParams lstParam = new ListParams();
        //lstParam.Add("@FromDate", Fromdate);
        lstParam.Add("@AsOnDate", param.AsOndate);
        lstParam.Add("@BCRefCode", param.BCRefCode);
        lstParam.Add("@AccID", param.AccID);
        _AccList = (AccrList)_AccList.GetAccountMappedList(lstParam);
        return _AccList;
    }

This is working good in jquery call.. And how to write the same C# code

This is what i tried

        Params param1 = new Params();
        param1.AsOndate = System.DateTime.Today;
        param1.AccID = 90000;
        param1.BCRefCode = 100;
        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("http://localhost:51093/");
        client.DefaultRequestHeaders.Accept.Add(new  MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.GetAsync("/api/account", param1, new JsonMediaTypeFormatter()).Result;
        if (response.IsSuccessStatusCode)
        {.....
        }

3 Answers 3

2

Got the answer and it worked for me

protected void btnGetdata_Click(object sender, EventArgs e)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:xxxx/");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        string param = "AsOnDate=" + System.DateTime.Today + "&AccID=" + 90000 + "&BCRefCode=" + 100;
        HttpResponseMessage response = client.GetAsync("/api/account?" + param, HttpCompletionOption.ResponseContentRead).Result;
        if (response.IsSuccessStatusCode)
        {
            var aa = response.Content.ReadAsAsync<object>().Result;
            object obj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<YourClassName>>(aa.ToString());
        }
    }

Thanks to all

Sign up to request clarification or add additional context in comments.

Comments

1

Use this method.

    string param = "AsOndate=" + System.DateTime.Today + "&AccID=" + 90000 + "&BCRefCode=" + 100;
    HttpResponseMessage response = client.GetAsync("/api/account?" + param,HttpCompletionOption.ResponseContentRead).Result;

Thanks.

2 Comments

i am getting error: Error The best overloaded method match for 'System.Net.Http.HttpClient.GetAsync(string, System.Net.Http.HttpCompletionOption)' has some invalid arguments
Its Ok Now. Its firing the correct method and returning some 'response'. How can we deserialize this response?
-1

continuing with the answer given by @felix

It will surely get the error as you have not changed the parameter for the api code :

    public IEnumerable GetAccountListForMapping(string param)
    {
        // Your Code
    }

and now extract that data from the 'param' string.

I hope this will work.

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.