在ASP.NET Core WebAPI中,您可以使用不同的路由和参数来定义多个HTTP GET方法。以下是一个使用不同路由和参数的示例:
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
// GET api/user
[HttpGet]
public ActionResult> Get()
{
return new string[] { "user1", "user2", "user3" };
}
// GET api/user/{id}
[HttpGet("{id}")]
public ActionResult Get(int id)
{
return $"user{id}";
}
// GET api/user/{id}/details
[HttpGet("{id}/details")]
public ActionResult GetDetails(int id)
{
return $"details for user{id}";
}
}
在上面的代码中,我们定义了三个HTTP GET方法:
[HttpGet]
特性,它将匹配路由api/user
,并返回一个字符串数组。[HttpGet("{id}")]
特性,它将匹配带有一个整数参数的路由,例如api/user/1
,并返回一个字符串。[HttpGet("{id}/details")]
特性,它将匹配带有一个整数参数的带有"/details"后缀的路由,例如api/user/1/details
,并返回一个字符串。您可以根据自己的需要定义更多的HTTP GET方法,只需使用不同的路由和参数即可。