使用 RoutingParameter 进行属性路由

Attribute Routing with RoutingParameter

我目前在 Web api 控制器中有一组使用属性路由的操作方法。

[RoutePrefix("Paper")]...

[Route("Shape/Octagon/{id:minlength(1)}]
public IEnumerable<sampleShape> PostactionName(string id){..}

[Route("Shape/Hexagon/{id:minlength(1)}]]
public IEnumerable<sampleShape> PostactionName(string id){..}

适用于以下 URI

api/Paper/Shape/Octagon/"1,2,3"
api/Paper/Shape/Hexagon/"3,2,1"

但是一旦id参数变长就不能用了。无论如何让路由使用参数 id 作为表单数据而不是 URI 的一部分,但仍然保留 Route 属性。

您可以使用 FromBody 属性让引擎知道参数将来自 post body

[RoutePrefix("Paper")]...

[Route("Shape/Octagon"}]
public IEnumerable<sampleShape> PostactionName([FromBody]string id){..}

[Route("Shape/Hexagon"}]]
public IEnumerable<sampleShape> PostactionName([FromBody]string id){..}