我来自一个沉重的 Java/Spring 背景,并试图将一些知识转移到 ASP.NET Core 6。
在 Spring 中RestController
,我可以根据查询参数的存在来路由请求。
因此,HttpRequest
带有 uri:/students?firstName=Kevin
的 a 可以路由到与带有 uri: 的 a 不同的控制器HttpRequest
方法/students
。
在 ASP.NET Core 6 中,在完成一些示例并阅读 Web API 文档后,我无法确定是否可以实现等价物。
这是我要实现的目标,这是否可以使用两种方法和路由配置来识别根据查询参数调用哪个控制器方法?
[ApiController]
[Route("Students")]
public class StudentHomeProfileController : ControllerBase
{
[HttpGet] //Route here when no parameters provided
public async Task<ActionResult<IEnumerable<Student>>> GetStudentAsync()
{
/* Code omitted */
}
[HttpGet] //Route here when firstName query param provided
public async Task<ActionResult<IEnumerable<Student>>> SearchStudentAsync([FromQuery] string firstName)
{
/* Code omitted */
}
}