WebApi参数用/

时间:2015-10-26 09:42:55

标签: c# asp.net-web-api

我的webapi路线是标准路线:

controller/action/{id}

在我的控制器中,相应的操作ID映射到字符串。 在应用程序中,用户可以配置Id。用户为其ID选择了以下格式:X / Y / Z。

因此,在请求元素时,请求是:

controller/action/X/Y/Z {remember 'X/Y/Z' is the id}

webapi返回404错误,我甚至无法通过调试进入控制器。

即使我在Id中编码code /,也会发生同样的事情

controller/action/X%2FY%2FZ {%2F being the encoding for /}

方法签名如下:

[Route("{reference}")]
[HttpGet]
public IHttpActionResult GetElementById(string reference = null)

如果ID为/在id值中,我如何将其作为参数发送?

2 个答案:

答案 0 :(得分:2)

使用可变数量的细分。请将您的WebApiConfig更改为:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{*id}",
                defaults: new { id = RouteParameter.Optional }
            );

答案 1 :(得分:0)

我建议使用属性路由。

[HttpGet, Route("api/dosomething/{x:int:min(1)}/{y:int:min(1)}/{z:int:min(1)}")]
public IHttpActionResult YourMethod(int x, int y, int z)
{

}
相关问题