I have to return a single property of a model inside GetAll() method in ASP.NET Core API. Here is my Model:
using System;
using System.Collections.Generic;
namespace ProjectWayneAPI.Models
{
public partial class Consignment
{
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public bool? Deleted { get; set; }
public string CustomerReference { get; set; }
public string ConsignmentNote { get; set; }
public Guid AccountId { get; set; }
public Guid BookedByUser { get; set; }
public string DescriptionToLeave { get; set; }
public virtual Account Account { get; set; }
public virtual User BookedByUserNavigation { get; set; }
public virtual User CheckedByUserNavigation { get; set; }
public virtual User ModifiedByUserNavigation { get; set; }
public virtual User QuotedByUserNavigation { get; set; }
}
}
I have to return only the ConsignmentNote(but I have to pull all of them) in my controller's method.
Here is my Controller:
[Route("api/[controller]")]
[ApiController]
public class ConsignmentsController : ControllerBase
{
private readonly WayneContext _context;
public ConsignmentsController(WayneContext context)
{
_context = context;
}
//GET: api/Consignment Notes
[Route("[connote]")]
[HttpGet]
public async Task<ActionResult<IEnumerable<Consignment>>> GetConsignmentNote()
{
var connote = await _context.Consignment
.Include(x => x.ConsignmentNote)
.ToListAsync();
return connote;
}
// GET: api/Consignments
[HttpGet]
public async Task<ActionResult<IEnumerable<Consignment>>> GetConsignment()
{
var consignments = await _context.Consignment
.Where(c => c.Deleted == false)
.Include(bu=> bu.BookedByUserNavigation)
.ToListAsync();
return consignments;
}
}
I have to return all the connotes with this method public async Task>> GetConsignmentNote(), if you check the linq query inside the method, that query is returning exception. And also how do I override the [HttpGet] in this case?