【发布时间】:2021-06-05 01:08:40
【问题描述】:
我尝试在HeroesController类中触发特定方法(第二个):
[HttpGet]
public IEnumerable<Hero> Get()
{
return Heroes;
}
[HttpGet("/name={term}")]
public IEnumerable<Hero> Get(string term)
{
return Heroes;
}
调用此网址后:
https://localhost:44375/heroes/?name=Spider
第一种方法被触发,而不是第二种。为什么呢?接收term参数的第二个如何触发?
【问题讨论】:
-
因为路由不匹配,所以需要使用url
https://localhost:44375/heroes/name=Spider——但是在pattern中使用=是不常见的。 -
是的,但我需要创建一个知道如何响应此特定 URL 的方法:localhost:44375/heroes/?name=Spider
-
所以你已经有了第一个
Get方法(正如你所说,它被调用了)。您想要的网址有一个名为query string的部分。Get方法应该类似于Get(string name)。第二个Get没有意义,去掉[HttpGet("/name={term}")]
标签: c# api asp.net-core postman httprequest