【发布时间】:2019-04-17 17:01:09
【问题描述】:
我很可能设置错了。但是我的 web api 控制器中的 get 方法。
[HttpGet]
[Route("{productId:guid}", Name = nameof(GetProduct))]
[ResponseType(typeof(Product))]
public async Task<IHttpActionResult> GetProduct([FromUri]GetProductRequest request)
在请求中我有 Guid ProductId
public class GetProductRequest
{
public const string ProductIdRequired = nameof(ProductId) + " cannot be empty";
[Required(ErrorMessage = ProductIdRequired)]
public Guid ProductId { get; set; }
}
我希望请求 url 看起来像 localhost/v1/product/123
但它是 localhost/v1/product/{ProductId}?productId=123&request.productId=123'
如果我放弃路线 {productId:guid} 我得到 localhos/v1/product?request.productId=123' 这也很奇怪。不知道为什么它在 url 中需要 request.productId。
【问题讨论】:
-
你需要用
[FromRoute]装饰房子 -
@DanielA.White 你只能在 asp.net core 中使用
[FromRoute]
标签: c# rest asp.net-web-api asp.net-web-api2