【发布时间】:2020-06-24 13:16:39
【问题描述】:
我想创建一个自定义的 get 方法,例如:https://localhost:4200/api/get/Echeanciers?idengin=2
我想得到所有“Echeancier”,其中 id 引擎等于 2。我不知道它是否可能。
我可以用这个sql请求解释清楚:
select * from Echeancier where id_engin = 2
这是我的 Echeancier 模型:
public partial class Echeancier
{
public int IdEcheancier { get; set; }
public string MoisEcheancier { get; set; }
public string Montant { get; set; }
public int IdEngin { get; set; }
public virtual Engin IdEnginNavigation { get; set; }
}
id 引擎是其他类的属性:
public partial class Engin
{
public Engin()
{
Echeancier = new HashSet<Echeancier>();
}
public int IdEngin { get; set; }
public int Matricule { get; set; }
public string NumSerie { get; set; }
public int? IdEcheancier { get; set; }
}
这是 Echeancier 控制器
[Route("api/[controller]")]
[ApiController]
public class EcheanciersController : ControllerBase
{
private readonly dbSecurityParkContext _context;
public EcheanciersController(dbSecurityParkContext context)
{
_context = context;
}
// GET: api/Echeanciers
[HttpGet]
public async Task<ActionResult<IEnumerable<Echeancier>>> GetEcheancier()
{
return await _context.Echeancier.ToListAsync();
}
// GET: api/Echeanciers/5
[HttpGet("{id}")]
public async Task<ActionResult<Echeancier>> GetEcheancier(int id)
{
var echeancier = await _context.Echeancier.FindAsync(id);
if (echeancier == null)
{
return NotFound();
}
return echeancier;
}
// PUT: api/Echeanciers/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPut("{id}")]
public async Task<IActionResult> PutEcheancier(int id, Echeancier echeancier)
{
if (id != echeancier.IdEcheancier)
{
return BadRequest();
}
_context.Entry(echeancier).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EcheancierExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Echeanciers
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
public async Task<ActionResult<Echeancier>> PostEcheancier(Echeancier echeancier)
{
_context.Echeancier.Add(echeancier);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (EcheancierExists(echeancier.IdEcheancier))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetEcheancier", new { id = echeancier.IdEcheancier }, echeancier);
}
// DELETE: api/Echeanciers/5
[HttpDelete("{id}")]
public async Task<ActionResult<Echeancier>> DeleteEcheancier(int id)
{
var echeancier = await _context.Echeancier.FindAsync(id);
if (echeancier == null)
{
return NotFound();
}
_context.Echeancier.Remove(echeancier);
await _context.SaveChangesAsync();
return echeancier;
}
private bool EcheancierExists(int id)
{
return _context.Echeancier.Any(e => e.IdEcheancier == id);
}
}
【问题讨论】: