gpt4 book ai didi

c# - 如何从 silverlight 应用程序调用 Web api

转载 作者:行者123 更新时间:2023-11-30 17:03:17 25 4
gpt4 key购买 nike

我是 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

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

最佳答案

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/

关于c# - 如何从 silverlight 应用程序调用 Web api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18778861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com