【问题标题】:Custom get method with .net core使用 .net 核心的自定义 get 方法
【发布时间】: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);
     }
 }

【问题讨论】:

    标签: .net api rest get


    【解决方案1】:

    你可以试试:

    [HttpGet]
    public async Task<ActionResult<IEnumerable<Echeancier>>> GetEcheancier([FromQuery] int? idengin)
    {
        var query = _context.Echeancier.AsQueryable();
        if(idengin.HasValue)
        {
             query = query.Where(item=> item.IdEngin == idengin.Value);
        }
        return await query.ToListAsync();
    }
    

    【讨论】:

    • 谢谢你的回答,我补充了,但它给了我一个错误:AmbiguousMatchException: The request match multiple endpoints.
    • @ClémentNguyen 不要添加,更新现有的// GET: api/Echeanciers [HttpGet] public async Task&lt;ActionResult&lt;IEnumerable&lt;Echeancier&gt;&gt;&gt; GetEcheancier()
    • @ClémentNguyen 哪个?
    • @ClémentNguyen 很乐意提供帮助!
    • 在我的 Echeanciers 服务中,如何添加这个 get 方法? getEcheanciers(){ return this.http.get(this.rootURL + "/Echeanciers"); }
    【解决方案2】:

    基本上你必须返回 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;
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      • 2018-01-20
      • 2016-06-02
      • 2020-11-18
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多