我有一个带有以下内容的测试控制器 -
public string Get(int id)
{
return "hi from test " + id;
}
[HttpGet]
public string Search(string text)
{
return "you searched for " + text;
}
我可以打电话
- http://localhost:58635/api/Test/2
- http://localhost:58635/api/Test?id=2
- http://localhost:58635/api/Test/Search?text=textToSearcFor
但不是http://localhost:58635/api/Test/Search/textToSearcFor
我的路线就像这篇文章中的路线
config.Routes.MapHttpRoute(
name: "ApiById",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = @"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Get" }
);
我究竟做错了什么?