【发布时间】:2021-10-20 04:01:38
【问题描述】:
我正在尝试不同的 REST API 设计。我用一个模型和两个控制器创建了一个新的 .Net 5 应用
public sealed record Todo(string Title, string OwnedBy, bool IsDone = false);
public sealed class TodoQueriesController : ControllerBase
{
[HttpGet("my-todo-by-title/{title}")]
[ActionName(nameof(GetTodoFromAuthenticatedUserByTitle))]
public ActionResult<Todo> GetTodoFromAuthenticatedUserByTitle([FromRoute] string title)
{
string authenticatedUsername = "foo";
return Ok(new Todo(title, authenticatedUsername));
}
}
public sealed class TodoCommandsController : ControllerBase
{
[HttpPost("assign-todo-to-me")]
public ActionResult<Todo> AssignTodoToAuthenticatedUser([FromBody] string title)
{
string authenticatedUsername = "foo";
return CreatedAtAction("???", new { title }, new Todo(title, authenticatedUsername));
}
}
目前我不知道要传递什么作为CreatedAtAction 的操作名称(以替换"???")
我试过了
nameof(TodoQueriesController.GetTodoFromAuthenticatedUserByTitle)"GetTodoFromAuthenticatedUserByTitle""my-todo-by-title"$"my-todo-by-title/{title}"
但不幸的是,我总是得到 500 分
System.InvalidOperationException:没有路由与提供的值匹配。
如何从另一个控制器引用端点操作?
【问题讨论】: