ASP.NET 核心 2.2 - 复杂路由的 CreatedAtRoute 方法失败
ASP.NET Core 2.2 - CreatedAtRoute method failure for complex routes
我有这个 AuthController,我想使用 CreatedAtRoute 方法来 运行 UsersController 中的一个操作 return 插入的用户,但它给出了这个错误:
InvalidOperationException: No route matches the supplied values.
此处显示了 AuthController。用户注册并在成功注册后调用 UsersController 的 GetUser 操作:
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IConfiguration _config;
private readonly ITokenService _tokenService;
private readonly IMapper _mapper;
private readonly UserManager<User> _userManager;
private readonly IUserService _userService;
private readonly SignInManager<User> _signInManager;
private readonly ILogger _logger;
public AuthController(IConfiguration config,
ITokenService tokenService,
IMapper mapper,
UserManager<User> userManager,
SignInManager<User> signInManager,
IUserService userService,
ILogger<AuthController> logger)
{
_tokenService = tokenService;
_userManager = userManager;
_signInManager = signInManager;
_mapper = mapper;
_config = config;
_userService = userService;
_logger = logger;
}
[AllowAnonymous]
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody]UserForRegisterDto userForRegisterDto)
{
_logger.LogInformation(LoggingEvents.InsertItem, "Registering user {UserName}", userForRegisterDto.UserName);
var userToCreate = _mapper.Map<User>(userForRegisterDto);
var result = await _userManager.CreateAsync(userToCreate,"1234567a" /*userForRegisterDto.Password*/);
var userToReturn = _mapper.Map<UserForLoginReturnDto>(userToCreate);
if (result.Succeeded)
{
_logger.LogInformation(LoggingEvents.InsertItem, "Registering user {UserName}", userForRegisterDto.UserName);
return CreatedAtRoute("GetUser",
new { controller = "Users", id = userToCreate.Id }, userToReturn);
}
_logger.LogError(LoggingEvents.InsertItem, "Registering user failed: {UserName}", userForRegisterDto.UserName);
return BadRequest(result.Errors);
}
}
而 UsersController 如下:
[Route("api/users/{userId}/users")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
private readonly IMapper _mapper;
private readonly UserManager<User> _userManager;
public UsersController(IUserService userService,
UserManager<User> userManager,
IMapper mapper)
{
_userService = userService;
_userManager = userManager;
_mapper = mapper;
}
[Authorize(Roles = "Admin, Manager, User")]
[HttpGet("{id}", Name = "GetUser")]
public async Task<IActionResult> GetUser(int id)
{
var isCurrentUser = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value) == id;
var user = await _userService.GetUser(id, isCurrentUser);
var userToReturn = _mapper.Map<UserForLoginReturnDto>(user);
return Ok(userToReturn);
}
}
如何在 CreatedAtRoute 方法中处理 UsersController?
这是路由问题。您缺少目标路线的值
return CreatedAtRoute("GetUser", new { id = userToCreate.Id, userId = {some value here} }, userToReturn);
我有这个 AuthController,我想使用 CreatedAtRoute 方法来 运行 UsersController 中的一个操作 return 插入的用户,但它给出了这个错误:
InvalidOperationException: No route matches the supplied values.
此处显示了 AuthController。用户注册并在成功注册后调用 UsersController 的 GetUser 操作:
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IConfiguration _config;
private readonly ITokenService _tokenService;
private readonly IMapper _mapper;
private readonly UserManager<User> _userManager;
private readonly IUserService _userService;
private readonly SignInManager<User> _signInManager;
private readonly ILogger _logger;
public AuthController(IConfiguration config,
ITokenService tokenService,
IMapper mapper,
UserManager<User> userManager,
SignInManager<User> signInManager,
IUserService userService,
ILogger<AuthController> logger)
{
_tokenService = tokenService;
_userManager = userManager;
_signInManager = signInManager;
_mapper = mapper;
_config = config;
_userService = userService;
_logger = logger;
}
[AllowAnonymous]
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody]UserForRegisterDto userForRegisterDto)
{
_logger.LogInformation(LoggingEvents.InsertItem, "Registering user {UserName}", userForRegisterDto.UserName);
var userToCreate = _mapper.Map<User>(userForRegisterDto);
var result = await _userManager.CreateAsync(userToCreate,"1234567a" /*userForRegisterDto.Password*/);
var userToReturn = _mapper.Map<UserForLoginReturnDto>(userToCreate);
if (result.Succeeded)
{
_logger.LogInformation(LoggingEvents.InsertItem, "Registering user {UserName}", userForRegisterDto.UserName);
return CreatedAtRoute("GetUser",
new { controller = "Users", id = userToCreate.Id }, userToReturn);
}
_logger.LogError(LoggingEvents.InsertItem, "Registering user failed: {UserName}", userForRegisterDto.UserName);
return BadRequest(result.Errors);
}
}
而 UsersController 如下:
[Route("api/users/{userId}/users")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
private readonly IMapper _mapper;
private readonly UserManager<User> _userManager;
public UsersController(IUserService userService,
UserManager<User> userManager,
IMapper mapper)
{
_userService = userService;
_userManager = userManager;
_mapper = mapper;
}
[Authorize(Roles = "Admin, Manager, User")]
[HttpGet("{id}", Name = "GetUser")]
public async Task<IActionResult> GetUser(int id)
{
var isCurrentUser = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value) == id;
var user = await _userService.GetUser(id, isCurrentUser);
var userToReturn = _mapper.Map<UserForLoginReturnDto>(user);
return Ok(userToReturn);
}
}
如何在 CreatedAtRoute 方法中处理 UsersController?
这是路由问题。您缺少目标路线的值
return CreatedAtRoute("GetUser", new { id = userToCreate.Id, userId = {some value here} }, userToReturn);