[英]How to enable count in OData and .NET Core 3.0 on ASP .NET Application?
When I make a request : http://localhost:5000/api/v1/users ?$count=true当我提出请求时: http://localhost:5000/api/v1/users ?$count=true
It returns the object and the values of its properties but it does not return the count of users:它返回对象及其属性的值,但不返回用户数:
This is my action in the controller:这是我在控制器中的操作:
/// <summary>
/// Gets the list of all available users in the system
/// </summary>
/// <returns></returns>
[HttpGet]
[EnableQuery]
public IEnumerable<UserDto> Get()
{
var users = dbContext.Users.ToList();
return users.Select(u =>
{
var dto = mapper.Map<UserDto>(u);
var claims = dbContext.UserClaims.Where(c => c.UserId == u.Id);
dto.FirstName = claims.FirstOrDefault(c => c.ClaimType == JwtClaimTypes.GivenName)?.ClaimValue;
dto.FamilyName = claims.FirstOrDefault(c => c.ClaimType == JwtClaimTypes.FamilyName)?.ClaimValue;
return dto;
}).ToList();
}
This is my startup config:这是我的启动配置:
services.AddOData();
app.UseMvc(router =>
{
router.EnableDependencyInjection();
router.Select().Expand().Filter().OrderBy().MaxTop(100).Count().EnableContinueOnErrorHeader();
router.MapODataServiceRoute("odata", "odata", GetEdmModel());
});
IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<IntentUser>("Users");
return odataBuilder.GetEdmModel();
}
Since I updated to OData 7.3.0 (and .Net Core 3.1), I have the same Problem.自从我更新到 OData 7.3.0(和 .Net Core 3.1)以来,我遇到了同样的问题。
If you use [Route(...)] and [ApiController] at the controler class, maybe thats the problem.如果您在控制器类中使用 [Route(...)] 和 [ApiController],也许这就是问题所在。 Vanish Route attribute and ApiController attribute: https://stackoverflow.com/a/58849806/12858107 Vanish Route 属性和 ApiController 属性: https ://stackoverflow.com/a/58849806/12858107
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.