3

我是 Web api 和 json 的新手。我不知道调用 WebApi 中的方法。以下方法在 Webapi 中:

[HttpGet]
public bool AddAccount([FromRoute]string accountname)
{
    try
    {
        BizFramework.Web.Model.Account data = new BizFramework.Web.Model.Account();              
        data.AccountGuid = Guid.NewGuid();
        data.AccountName = accountname;
        data.ParentAccountID = 0;
        data.AccountTypeID = 13;
        data.AccountNo = "8060";
        data.Active = true;
        data.HierarchyLevel = 1;
        data.CashFlowID = 3;
        data.OpeningBalanceDate = DateTime.Now;
        data.IscashBasis = true;
        data.Createdby = "BAOwner";
        data.CreatedDatetime = DateTime.Now;
        data.Modifiedby = "BAOwner";
        data.ModifiedDatetime = DateTime.Now;
        BA.AddToAccounts(data);
        BA.SaveChanges();
        return true;
    }
    catch (Exception ex)
    {
        File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "log.txt", ex.ToString());
        return false;
    }
} 

使用此链接:

http://example.com/CustomerPortalService12/AddAccount/AccountsReceivable

我可以补充。但是通过编码我该如何添加?

4

1 回答 1

2
public void AddAccount(string accountname, Action<bool> success, Action<bool> failure)
    { var client = new RestClient("http://[localhost]/CustomerPortalService12");
        // client.Authenticator = new HttpBasicAuthenticator(username, password);

        var request = new RestRequest("AddAccount/AccountsReceivable/" + accountname, Method.GET);

        client.ExecuteAsync(request, (response) =>
        {
            if (response.ResponseStatus == ResponseStatus.Error)
            {
                failure(response.ErrorMessage);
            }
            else
            {
                var result= JsonConvert.DeserializeObject<bool>(response.Content);
                success(result);
            }
        }); }

调用这个函数

AddAccount("accountname",
               (item) => Dispatcher.BeginInvoke(() =>
               {

                   MessageBox.Show("Done!");

               }),
                   (error) => Dispatcher.BeginInvoke(() =>
                   {
                       MessageBox.Show(error);
                   }));

我通过使用 RestSharp 做到了这一点。

有关更多详细信息,请转到此处

http://restsharp.org/

于 2013-09-13T06:09:48.773 回答