我正在尝试构建一个Web API,该API将接受两个参数。然而,在调用API时,它总是没有任何参数就命中了该方法。
我按照这里的说明进行操作,但无法弄清楚为什么它不起作用。
我使用'PostMaster' Chrome扩展程序发送的请求如下:
控制器内的方法如下:
webApiConfig 没有太多更改,除了它现在接受两个参数:
根据文档,我的理解是webapiconfig不需要更改。这是我收到的错误信息。
我使用'PostMaster' Chrome扩展程序发送的请求如下:
http://localhost:51403/api/test/title/bf
对于上述请求,我希望第一个方法被命中,但实际上却到达了第二个方法。控制器内的方法如下:
// Get : api/test/type/slug
public void Get(string type,string slug){
//Doesn't reach here
}
// Get : api/test
public void Get() {
// Reaches here even when the api called is GET api/test/type/slug
}
webApiConfig 没有太多更改,除了它现在接受两个参数:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id1}/{id2}",
defaults: new { id1 = RouteParameter.Optional, id2 = RouteParameter.Optional }
);
}
根据文档,我的理解是webapiconfig不需要更改。这是我收到的错误信息。
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:51403/api/test/title/bf'.",
"MessageDetail":"No action was found on the controller 'test' that matches the request."}