这是我的路由配置:
config.Routes.MapHttpRoute( name: "ActionApi",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional } );
而且,这是我的控制器:
public class ProductsController : ApiController { [AcceptVerbs("Get")] public object GetProducts() { // return all products... } [AcceptVerbs("Get")] public object Product(string name) { // return the Product with the given name... } }
当我尝试api / Products / GetProducts /,它的作品. api /产品/产品?name = test也可以使用,但是api /产品/产品/测试不起作用.我究竟做错了什么?
更新:
以下是当我尝试api /产品/产品/测试时所得到的:
{ "Message": "No HTTP resource was found that matches the request URI 'http://localhost:42676/api/Products/Product/test'.","MessageDetail": "No action was found on the controller 'Products' that matches the request." }
解决方法@H_301_18@
这是因为您的路由设置及其默认值.你有两个选择.
1)通过更改路由设置以匹配Product()参数以匹配URI.
config.Routes.MapHttpRoute(
name: "ActionApi",routeTemplate: "api/{controller}/{action}/{name}",// removed id and used name
defaults: new { name = RouteParameter.Optional }
);
public object Product([FromUri(Name = "id")]string name){
// return the Product with the given name
}
1)通过更改路由设置以匹配Product()参数以匹配URI.
config.Routes.MapHttpRoute( name: "ActionApi",routeTemplate: "api/{controller}/{action}/{name}",// removed id and used name defaults: new { name = RouteParameter.Optional } );
public object Product([FromUri(Name = "id")]string name){ // return the Product with the given name }