我的 WebApi 有一個用于具有以下類的應用程式的表:
namespace Models.Public
{
[Index(nameof(UUID), nameof(UID), IsUnique = true)]
public class Application
{
public Application()
{
this.UUID = new Guid();
}
public int ID { get; set; }
public Guid UUID { get; set; }
[Required]
public string UID { get; set; }
public string Publisher { get; set; }
public string Name { get; set; }
public string Version { get; set; }
}
}
該欄位UUID和ID是唯一的,因此我能夠生成所需的HttpGet命令來獲得匹配的結果。
但是,我正在嘗試獲取與該Publisher欄位匹配的所有專案的 IEnumerable 物件。也就是說,回傳所有以“Google”為發布者的物件。
我的嘗試沒有成功,我希望得到一些建議來修復我的代碼:
// GET: api/Application/<publisher>
[HttpGet("{publisher}")]
public async Task<ActionResult<IEnumerable<Application>>> GetApplication(string publisher)
{
var application = await _context.Application.ToListAsync(publisher);
if (application == null)
{
return NotFound();
}
return await _context.Application.ToListAsync();
}
Publisher不是唯一值,因此我希望能夠將所有專案作為 JSON 物件回傳,這些專案具有我在串列中鍵入的任何 Publisher 。如果沒有匹配項,則使用NotFound();.
uj5u.com熱心網友回復:
您將需要使用過濾器.Where,.Contains
// GET: api/Application/<publisher>
[HttpGet("{publisher}")]
public async Task<ActionResult<IEnumerable<ApplicationData>>> GetApplication(string publisher)
{
var applications = _context.Application.Where(a=>a.Publisher.Contains(publisher)));
/* You could also use == for exact match
var applications = _context.Application.Where(a=>a.Publisher == publisher));
*/
if (applications.Count() == 0)
{
return NotFound();
}
return await applications.ToListAsync();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/440800.html
標籤:C# asp.net 核心 asp.net-web-api 昂首阔步
下一篇:Moq-模擬方法回傳null
