1

目前我有以下用户模型

public class User {
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Email { get; set; }

    [Required]
    public string Password { get; set; }

}

以及以下接收电子邮件和密码的 Auth Endpoint:

[AllowAnonymous]
[HttpPost("[action]")]
public IActionResult Authenticate([FromBody] User operatorParam) {
    var user = _userService.Authenticate(operatorParam.Email, operatorParam.Password);

    if (user == null) {
        return BadRequest(new {message = "Email or password is incorrect"});
    }

    return Ok(user);
}

Swagger 自动文档生成以下内容:

在此处输入图像描述

有什么方法可以从示例中删除 ID、名称和令牌?因为在此特定端点中只需要电子邮件和密码。

4

1 回答 1

1

为请求正文创建另一个模型

    public class UserRequest {
        public string Name { get; set; } //you can also remove this
        public string Email { get; set; }
        public string Password { get; set; }

    }

    [AllowAnonymous]
    [HttpPost("[action]")]
    public IActionResult Authenticate([FromBody] UserRequest operatorParam) {
        var user = _userService.Authenticate(operatorParam.Email, operatorParam.Password);

        if (user == null) {
            return BadRequest(new {message = "Email or password is incorrect"});
        }

        return Ok(user);
    }

有一个例子你可以按照这个例子

于 2019-09-09T18:07:09.210 回答