首页 > 解决方案 > 使用 .net 核心的自定义获取方法

问题描述

我想创建一个自定义 get 方法,例如:https://localhost:4200/api/get/Echeanciers?idengin=2 我想获取id 引擎等于 2 的所有“Echeancier”。我不知道它是否可能。我可以用这个 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);
     }
 }

标签: .netapirestget

解决方案


基本上你必须返回 Echeancier 列表:

public async Task<ActionResult<List<Echeancier>>> GetEcheancier(int id)
{
    var echeancier = await _context.Echeancier.Where(item=> item.id== id).ToListAsync();

    if (echeancier == null)
    {
        return NotFound();
    }

    return echeancier;
}

您还可以使用工厂模式将数据库操作与控制器本身分开。我希望它可以帮助你。


推荐阅读