【发布时间】:2018-10-27 14:34:54
【问题描述】:
我们可以在 ASP.NET Core 的 Route 模板中使用破折号 (-) 吗?
// GET: api/customers/5/orders?active-orders=true
[Route("customers/{customer-id}/orders")]
public IActionResult GetCustomerOrders(int customerId, bool activeOrders)
{
.
.
.
}
(上面的代码不行)
【问题讨论】:
-
但你为什么要这么做?
-
是的,好问题。你为什么要首先这样做?路由参数通常直接映射到变量名,所以
Route("customers/{customerid}/orders")应该可以工作,因为那是你的变量名。不过你可以试试public IActionResult GetCustomerOrders([FromRoute(Name = "customer-id")]int customerId, bool activeOrders) -
@Tseng - 1. “我们建议您在 URL 中使用连字符 (-) 而不是下划线 (_)。” support.google.com/webmasters/answer/76329?hl=en 2. 我试过你的解决方案,它有效。我也可以有
..., [FromQuery(Name = "active-orders")] bool activeOrders)。极好的。问题解决了。为了其他人,您能否发布这 2 个 cmets 作为答案? -
@AmanB - 建议在 URL 中使用破折号而不是驼峰式或连字符。见:stackoverflow.com/a/2318376/538387
-
@AmanB作为设计师我觉得好看多了,而且url就是客户看到的东西
标签: asp.net-core asp.net-core-mvc asp.net-core-2.1