这是我的路由配置:
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/Products/Product?name=test
也可以工作,但是api/Products/Product/test
不行。我做错了什么?
更新:
当我尝试api/Products/Product/test
时,这是我得到的:
{
"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."
}
public object Product([FromUri(Name = "id")]string name)
。 - ataravati