ASP.NET Core Web API 在 API GET 请求中检索空白记录
Posted
技术标签:
【中文标题】ASP.NET Core Web API 在 API GET 请求中检索空白记录【英文标题】:ASP.NET Core Web API retrieves blank record in API GET Request 【发布时间】:2022-01-14 21:50:04 【问题描述】:我正在使用 ASP.NET Core Web API。我有这些模型:
public abstract class EntityBase
[Key]
public int Id get; set;
public class Mandate : EntityBase
public int? MerchantId get; set;
public DateTime DueDate get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;
[ForeignKey("MerchantId")]
public virtual Merchant Merchant get; set;
模型:授权
视图模型(Dto):
public class MandateGetDto
public int? MerchantId get; set;
public DateTime DueDate get; set;
public DateTime StartDate get; set;
public DateTime EndDate get; set;
IBaseRepository:
public interface IBaseRepository<T> where T : BaseEntity
Task<IEnumerable<T>> GetAll();
bool EntityExists(long id);
基础存储库:
public class BaseRepository<T> : IBaseRepository<T> where T : AuditableBaseEntity
private readonly DDMDbContext _context;
private DbSet<T> _entities;
public BaseRepository(DDMDbContext context)
_context = context;
_entities = context.Set<T>();
public async Task<IEnumerable<T>> GetAll()
var list = await _entities.ToListAsync();
return list;
public bool EntityExists(long id)
return _entities.Any(x => x.Id == id);
实体映射器:
public MandateGetDto FromMandateToMandateGetDto(Mandate mandate)
MandateGetDto mandateDto = new MandateGetDto();
mandateDto.MerchantId = mandate.MerchantId;
mandateDto.DueDate = mandate.DueDate;
mandateDto.StartDate = mandate.StartDate;
mandateDto.EndDate = mandate.EndDate;
return mandateDto;
工作单元:
public class UnitOfWork : IUnitOfWork
private readonly DContext _context;
public UnitOfWork(DContext context)
_context = context;
#region Mandate
private readonly IBaseRepository<Mandate> _mandateRepository;
public IBaseRepository<Mandate> MandateRepository => _mandateRepository ?? new BaseRepository<Mandate>(_context);
# endregion
public void Dispose()
if (_context != null)
_context.Dispose();
下面是我为 Mandate 服务编写的代码,它检索所有的授权记录。
授权服务:
public async Task<ResponsePagination<GenericPagination<MandateGetDto>>> GetAll(int page, int sizeByPage)
string nextRoute = null, previousRoute = null;
IEnumerable<Mandate> data = await _unitOfWork.MandateRepository.GetAll();
var mapper = new EntityMapper();
var mandatesDto = data.Select(m => mapper.FromMandateToMandateGetDto(m)).ToList();
GenericPagination<MandateGetDto> objGenericPagination = GenericPagination<MandateGetDto>.Create(mandatesDto, page, sizeByPage);
ResponsePagination<GenericPagination<MandateGetDto>> response = new ResponsePagination<GenericPagination<MandateGetDto>>(objGenericPagination);
response.CurrentPage = objGenericPagination.CurrentPage;
response.HasNextPage = objGenericPagination.HasNextPage;
response.HasPreviousPage = objGenericPagination.HasPreviousPage;
response.PageSize = objGenericPagination.PageSize;
response.TotalPages = objGenericPagination.TotalPages;
response.TotalRecords = objGenericPagination.TotalRecords;
response.Data = objGenericPagination;
if (response.HasNextPage)
nextRoute = $"/mandates?page=(page + 1)";
response.NextPageUrl = _uriPaginationService.GetPaginationUri(page, nextRoute).ToString();
else
response.NextPageUrl = null;
if (response.HasPreviousPage)
previousRoute = $"/mandates?page=(page - 1)";
response.PreviousPageUrl = _uriPaginationService.GetPaginationUri(page, previousRoute).ToString();
else
response.PreviousPageUrl = null;
return response;
public async Task<IEnumerable<Mandate>> GetMandates()
return await _unitOfWork.MandateRepository.GetAll();
startup.cs:
services.AddTransient<IMandateService, MandateService>();
控制器:
[Produces("application/json")]
[ApiController]
[ApiVersion("1.0")]
public class AdminController : ControllerBase
private readonly IMerchantService _merchantService;
private readonly DDMDbContext _context;
public AdminController(DDMDbContext context, IMerchantService merchantService)
_merchantService = merchantService;
_context = context;
[HttpGet("mandates")]
[Authorize]
public async Task<ResponsePagination<GenericPagination<MandateGetDto>>> GetAllMyMandates(int page, int sizeByPage)
return await _mandateService.GetAll(page, sizeByPage);
当我将 POSTMAN 用于获取请求时,我得到了以下响应:
"current_page": 0,
"page_size": 0,
"total_pages": -2147483648,
"total_records": 1,
"has_next_page": false,
"has_previous_page": false,
"data": []
数据为空白,总记录为 1。
我该如何解决这个问题?
谢谢
【问题讨论】:
【参考方案1】:您没有向我们展示您的代码的主要部分,即控制器
控制器 是您的 Web API 工作的原因。因此,如果您配置错误,它应该首先出现在控制器中,然后出现在其他地方。
向我们展示控制器的代码。
编辑
我不确定 DbSet 是否适合这种用法,但使用上下文对象对我有用。
你应该使用
_context.MandateOrWhateverElseItIs.ToListAsync();
而不是使用
_entities.ToListAsync();
使用 DbSet 可能是个问题,我建议使用上下文。我的应用程序在上下文中运行良好。
编辑
这是您在 BaseRepository 中的代码
public class BaseRepository<T> : IBaseRepository<T> where T : AuditableBaseEntity
private readonly DDMDbContext _context;
private DbSet<T> _entities;
public BaseRepository(DDMDbContext context)
_context = context;
_entities = context.Set<T>();
public async Task<IEnumerable<T>> GetAll()
var list = await _entities.ToListAsync();
return list;
public bool EntityExists(long id)
return _entities.Any(x => x.Id == id);
你应该把它改成什么
public class BaseRepository<T> : IBaseRepository<T> where T : AuditableBaseEntity
private readonly DDMDbContext _context;
private DbSet<T> _entities;
public BaseRepository(DDMDbContext context)
_context = context;
_entities = context.Set<T>();
public async Task<IEnumerable<T>> GetAll()
//Changes are here
var list = await _context.EntitiesOrWhateverElseItIs.ToListAsync(); //I don't know what the option is, you can look it inside the autocomplete. Be sure to change EntitiesOrWhateverElseItIs with the option you see in autocomplete menu
return list;
public bool EntityExists(long id)
return _entities.Any(x => x.Id == id);
【讨论】:
我已将控制器添加到代码中 您的输出似乎合法,我认为问题出在存储库中 @mike 存储库代码中的 _entities 是什么? @问题可能出在您的存储库代码或 MandateService 代码中mandatesDto = data.Select(m => mapper.FromMandateToMandateGetDto(m)).ToList();
我在代码中更新了它。我已经用了几天了以上是关于ASP.NET Core Web API 在 API GET 请求中检索空白记录的主要内容,如果未能解决你的问题,请参考以下文章
将文件从 ASP.NET Core Web api 发布到另一个 ASP.NET Core Web api