在 ASP.NET Core Web API 中的 HttpGet 方法中返回模型的单个 属性
Returning a single property of a Model in HttpGet Method in ASP.NET Core Web API
我必须 return ASP.NET Core API 中的 GetAll() 方法中的单个模型 属性。
这是我的模型:
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; }
}
}
我必须 return 在我的控制器的方法中只有 ConsignmentNote(但我必须拉出所有的)。
这是我的控制器:
[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;
}
}
我必须 return 使用此方法 public 异步任务>> GetConsignmentNote() 的所有含义,如果您检查方法,该查询是 returning 异常。还有在这种情况下如何覆盖 [HttpGet]?
嗨,如果你尝试这样的事情怎么办:
//[Route("[connote]")] < --remove route and call your method GET to verride httpget .. it will match the api/Consignments [GET]
//[HttpGet]
public async Task<ActionResult<IEnumerable<ConsignmentNote>>> Get()
{
var connote = await _context.Consignment
.Include(x => x.ConsignmentNote)
.ToListAsync();
return connote.Select(xx=>xx.ConsignmentNote).ToList();
}
希望对你有帮助!!
Include
用于include related properties(即您的导航属性,例如Account
)而不用于select正常属性的子集,如 ConsignmentNote
。
如果你想要的只是 ConsignmentNote
属性 那么你应该 Select
一个新的 Consignment
并且只填充那个特定的 属性:
var connote = await _context.Consignment
.Select(x => new Consignment
{
ConsignmentNote = x.ConsignmentNote
})
.ToListAsync();
return connote; // List<Consignment>
但请注意,这仍然是 select 一个 大部分为空的对象 。如果您想要的只是一个仅包含 string
值的列表,那么您可以通过 select 直接 属性 并更改 select 来实现。 =32=]类型:
public async Task<ActionResult<IEnumerable<string>>> GetConsignmentNote()
{
var connote = await _context.Consignment
.Select(x => x.ConsignmentNote)
.ToListAsync();
return connote; // List<string>
}
我必须 return ASP.NET Core API 中的 GetAll() 方法中的单个模型 属性。 这是我的模型:
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; }
}
}
我必须 return 在我的控制器的方法中只有 ConsignmentNote(但我必须拉出所有的)。
这是我的控制器:
[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;
}
}
我必须 return 使用此方法 public 异步任务>> GetConsignmentNote() 的所有含义,如果您检查方法,该查询是 returning 异常。还有在这种情况下如何覆盖 [HttpGet]?
嗨,如果你尝试这样的事情怎么办:
//[Route("[connote]")] < --remove route and call your method GET to verride httpget .. it will match the api/Consignments [GET]
//[HttpGet]
public async Task<ActionResult<IEnumerable<ConsignmentNote>>> Get()
{
var connote = await _context.Consignment
.Include(x => x.ConsignmentNote)
.ToListAsync();
return connote.Select(xx=>xx.ConsignmentNote).ToList();
}
希望对你有帮助!!
Include
用于include related properties(即您的导航属性,例如Account
)而不用于select正常属性的子集,如 ConsignmentNote
。
如果你想要的只是 ConsignmentNote
属性 那么你应该 Select
一个新的 Consignment
并且只填充那个特定的 属性:
var connote = await _context.Consignment
.Select(x => new Consignment
{
ConsignmentNote = x.ConsignmentNote
})
.ToListAsync();
return connote; // List<Consignment>
但请注意,这仍然是 select 一个 大部分为空的对象 。如果您想要的只是一个仅包含 string
值的列表,那么您可以通过 select 直接 属性 并更改 select 来实现。 =32=]类型:
public async Task<ActionResult<IEnumerable<string>>> GetConsignmentNote()
{
var connote = await _context.Consignment
.Select(x => x.ConsignmentNote)
.ToListAsync();
return connote; // List<string>
}