Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3672

Custom get method with .net core

$
0
0

I want to create a custom get method such as: https://localhost:4200/api/get/Echeanciers?idengin=2I want to get all "Echeancier" where id engin equals to 2. I don't know if its possible.I can explain clearly with this sql request:

select * from Echeancier where id_engin = 2

Here is my Echeancier models:

 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 engin is a property from an other class:

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; }}

This is the Echeancier controller

 [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);     } }

Viewing all articles
Browse latest Browse all 3672

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>