2

我可以在客户端调用 Web api 方法,现在我想用 c# 代码实现它。在这里,我正在编写我的 jquery 代码。

$(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
          });
});

我正在调用 web api 方法

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;
    }

这在 jquery 调用中效果很好。以及如何编写相同的 C# 代码

这是我试过的

        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)
        {.....
        }
4

3 回答 3

2

得到了答案,它对我有用

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());
        }
    }

谢谢大家

于 2013-10-10T09:37:57.067 回答
1

使用此方法。

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

谢谢。

于 2013-10-10T07:17:29.503 回答
-1

继续@felix给出的答案

它肯定会出现错误,因为您没有更改 api 代码的参数:

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

现在从“参数”字符串中提取该数据。

我希望这会奏效。

于 2013-10-10T08:12:34.387 回答