.NET Core OData 操作参数为空

.NET Core OData Action Parameter Null

我的 Odata 操作参数没有解析/反序列化。

我正在使用 dotnet core 2.2 来显示 OData 控制器。

我需要执行一个无界操作。 OData 路由引擎未反序列化操作参数 (UserDto userDto):

    [AllowAnonymous]
    [HttpPost]
    [ODataRoute(Routing.Endpoints.UserRoutes.AUTHENTICATE)]
    public async Task<IActionResult> Authenticate(UserDto userDto)
    {
        var user = await _userService.Authenticate(userDto?.Username, userDto?.Password);

        if (user == null)
            return BadRequest("Username or password is incorrect");

        var dto = Mapper.Map<UserDto>(user);

        return Ok(dto);
    }

这是我的配置:

         app.UseMvc(routeBuilder =>
        {
            var odataBuilder = new ODataConventionModelBuilder(app.ApplicationServices);
            odataBuilder.EnableLowerCamelCase();

            odataBuilder.EntitySet<BookDto>(nameof(Book));
            odataBuilder.EntitySet<UserDto>(nameof(User));

            var authenticate = odataBuilder.Action(Routing.Endpoints.UserRoutes.AUTHENTICATE);
            authenticate.Parameter<UserDto>("userDto");

            routeBuilder.Select().Expand().Filter().OrderBy().Count().MaxTop(int.MaxValue);
            routeBuilder.MapODataServiceRoute("odata", string.Empty, odataBuilder.GetEdmModel());
        });

这是 UserDto:

   public class UserDto
   {
       [Key] public Guid Id { get; set; }

       public string Username { get; set; }
       public string Password { get; set; }
       public string Token { get; set; }
   }

当我post:

操作由路由引擎解析 - 但参数没有 "Username" 和 "Password" 值:

如果我在参数上使用 [FromBody] 属性 - "userDto" 参数为空:

架构似乎正确:

<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
    <Action Name="authenticate">
         <Parameter Name="userDto" Type="ExampleApi.Dto.UserDto"/>
    </Action>
    <EntityContainer Name="Container">
         <EntitySet Name="Book" EntityType="ExampleApi.Dto.BookDto"/>
         <EntitySet Name="User" EntityType="ExampleApi.Dto.UserDto"/>
          <ActionImport Name="authenticate" Action="Default.authenticate"/>
    </EntityContainer>
 </Schema>

我尝试过以下操作:Action Parameter Support

甚至微软的版本(尽管已过时):Actions and Functions in OData

一整天都在为此苦思冥想...

您可以仅使用简单的 WebApi 属性来实现身份验证

 public class UserController : ODataController
 {
    [AllowAnonymous]
    [HttpPost("user/auth")]
    public async Task<IActionResult> Authenticate([FromBody] UserDto userDto)
    {
        return Ok(userDto);
    }
 }