[英]Web API [FromUri] Get Method Mapping
I could very well be setting this up wrong. 我很可能会设置错误。 But my get method in my web api controller.
但是我的Web api控制器中的get方法。
[HttpGet]
[Route("{productId:guid}", Name = nameof(GetProduct))]
[ResponseType(typeof(Product))]
public async Task<IHttpActionResult> GetProduct([FromUri]GetProductRequest request)
inside the request i have the Guid ProductId 在请求中,我有Guid ProductId
public class GetProductRequest
{
public const string ProductIdRequired = nameof(ProductId) + " cannot be empty";
[Required(ErrorMessage = ProductIdRequired)]
public Guid ProductId { get; set; }
}
I expected the request url to look something like localhost/v1/product/123 我希望请求网址看起来像localhost / v1 / product / 123
but it is localhost/v1/product/{ProductId}?productId=123&request.productId=123' 但它是localhost / v1 / product / {ProductId}?productId = 123&request.productId = 123'
if i drop the Route {productId:guid} i get localhos/v1/product?request.productId=123' which is also strange. 如果我放弃路线{productId:guid},我会得到localhos / v1 / product?request.productId = 123',这也很奇怪。 Not sure why it wants request.productId in the url.
不确定为什么要在URL中使用request.productId。
You've got two places where you're instructing MVC to parse an input value: The [Route]
attribute, plus the [FromUri]
attribute on the controller method parameter. 您在两个地方指示MVC解析输入值:
[Route]
属性,以及控制器方法参数上的[FromUri]
属性。
Try this: 尝试这个:
[FromUri]
from the controller method. [FromUri]
。 Guid productId
, which matches the value of your attribute route configuration [Route({productId:guid})]
. Guid productId
,它与属性路由配置[Route({productId:guid})]
的值匹配。 You should then see it respond to requests at: localhost/v1/product/{productId}
然后,您应该看到它响应以下请求:
localhost/v1/product/{productId}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.