【发布时间】:2015-08-14 18:43:33
【问题描述】:
我已使用此处选择的答案:Routing based on query string parameter name 来构建我的路线,但它们没有按预期工作:
我的路线:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}",
defaults: new { controller = "Products" }
);
我的行动:
public string GetProductById(int id) {}
public string GetProductByIsbn(string isbn) {}
我正在尝试通过以下方式调用这些:
localhost:60819/api/products/id=33 //doesn't work
localhost:60819/api/products/33 //does work
和
http://localhost:60819/api/products/isbn=9781408845240 //doesn't work
http://localhost:60819/api/products/testString //test with a definite string - doesn't work - still tries to use GetProductById(int id)
两个不起作用的错误是相同的:
<Error><Message>The request is invalid.</Message>
<MessageDetail>
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String GetProductById(Int32)' in 'BB_WebApi.Controllers.ProductsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
</MessageDetail>
</Error>
似乎认为 id 没有被传入...?
我已经阅读了所有的 msdn 文档,但我似乎在某处遗漏了一些东西。谁能看出我哪里出错了?
【问题讨论】: