alFadorMX
alFadorMX

Reputation: 85

WebApi parameters with /

My webapi route is the standard one:

controller/action/{id}

In my controller, the corresponding action id is mapped to a string. In the application, an Id can be configured by the user. A user selected the following format for his ids : X/Y/Z.

So, when requesting the element, the request is:

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

webapi returns a 404 error and I cannot even step into the controller with debug.

The same happens even if I encodecode / in the Id like

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

The method signature is as follows:

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

How do I send Id's as parameter when they have / in the id value?

Upvotes: 1

Views: 102

Answers (2)

Stephen Brickner
Stephen Brickner

Reputation: 2602

I would suggest using attribute routing.

[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)
{

}

Upvotes: 0

Roman Marusyk
Roman Marusyk

Reputation: 24619

Use Variable Number of Segments. Please change your WebApiConfig to:

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

Upvotes: 2

Related Questions