我有一个带 url 的 MVC Controller ,它有一个参数绑定(bind),里面有方括号
public Product GetProduct([Bind(Prefix = "product[productid]") int id)
它曾经像
这样与请求 url 一起工作http://localhost:8080/Product/GetProduct?product[productid] = 1001
。
我能够在 MVC Controller 中获取 id
的值作为 1001
。不幸的是,我应该将其更改为 web api Controller ,并且我有这样定义的 api Controller 方法
public Task<Product> GetProduct([FromUri(Name=product[productid])] int id)
为此,我收到了具有相同请求 url 的 404 响应。 [FromUrl]
使用不带大括号的参数名称
定义为
public Task<Product> GetProduct([FromUri(Name=productid)] int id)
和类似的 url
http://localhost:8080/Product/GetProduct?productid = 1001
除了用大括号解析请求 url 并获取值之外,是否有任何解决方法。
最佳答案
您的网址是正确的。您应该按以下方式修复 Controller :
public Task<Product> GetProduct([FromUri(Name=product.productid)] int id)
我真的不知道为什么会这样。
关于asp.net-web-api2 - WebApi FromUri 用大括号绑定(bind)url参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36816225/