asp.net-web-api2 - WebApi FromUri 用大括号绑定(bind)url参数

标签 asp.net-web-api2

我有一个带 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/

相关文章:

c# - 从 Web API 2 响应中删除 Cookie

c# - 由于 CORS,Angular 请求无法访问 API 控制

asp.net-web-api2 - 如何在 Asp.net Web Api 2 - Owin - ApiController 中获取自定义声明值。

asp.net-mvc-5 - 同一域中的 Web API 2 项目和 MVC 5 网站项目

c# - 无法等待任务 <MyObject>

asp.net - BufferedMediaTypeFormatter HttpContent 不包含请求中发送的所有 header

asp.net-mvc - 使用 OWIN 和 app.UseWebApi 在 Web API 中继续使用 cookie 进行身份验证

kendo-ui - Web API + OData - PATCH 请求 400 错误

c# - 在没有 Entity Framework 的情况下使用带有 OData 提要 C# 的存储过程进行自定义分页

javascript - Json参数转换继承Web api 2