【发布时间】:2020-10-15 19:49:15
【问题描述】:
我们有一个用 DotNet Core 3.1.402 编写的 Web API(我是 DotNet Core 和 WebAPI 的新手)。
我们使用 SqlKata 进行数据库处理。
我们有一个 Account 模型,其中包含 AccountID、AccountName、AccountNumber 等。
我们希望通过不同的属性获取一个帐户,例如:通过 AccountID、通过 AccountName、通过 AccountNumber。
我们如何做到这一点,以便我们不需要为每个属性单独的 HttpGet(这样我们就不必为不同的属性重复相同的代码)?
这是我们在AccountsController中通过AccountID获取帐号的HttpGet
public class AccountsController : ControllerBase
{
private readonly IAccountRepository _accountRepository;
[HttpGet("{AccountID}")]
public Account GetAccount(int AccountID)
{
var result = _accountRepository.GetAccount(AccountID);
return result;
}
这是 AccountRepository.cs 中的代码
public Account GetAccount(int accountID)
{
var result = _db.Query("MyAccountTable").Where("AccountID", accountID).FirstOrDefault<Account>();
return result;
}
这是 Account 类
namespace MyApi.Models
{
public class Account
{
public string AccountID { get; set; }
public string AccountName { get; set; }
public string AccountNumber { get; set; }
// other attributes
}
}
谢谢。
【问题讨论】:
标签: c# asp.net-web-api .net-core asp.net-core-webapi http-get