1

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?

alFadorMX
  • 85
  • 8
  • If you are using WebApi2, check your global routing in Application_Start(). Make sure there is only one entry like this: config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); – Paul Oct 26 '15 at 09:45
  • Please post your action method signature. Also you mean "X/Y/Z" together is the string passed to `id` parameter of your action? – Siva Gopal Oct 26 '15 at 09:48
  • 1
    My understanding is you have '/' in you id.. It is not wise to have it as data in your urls.. read this: http://stackoverflow.com/questions/3235219/urlencoded-forward-slash-is-breaking-url – Abdel Raoof Olakara Oct 26 '15 at 09:52
  • @abdel, it was not intentional, the id we use in the api is a customer created reference. On the mvc and web forms application we use a numeric id to find elements but we don't give this numerical id to the customers. When a customer creates an element, they have to write a 'customer id' of sorts {stored as string}. When the system was designed, many years ago, they didn't anticicipate api's like this. Potentially, many customer ids have been created with the caracter '/'. The api is being created to be used by the customer and should support the customer id, not the numerical id. – alFadorMX Oct 27 '15 at 10:34

2 Answers2

2

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

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{*id}",
                defaults: new { id = RouteParameter.Optional }
            );
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
0

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

}
Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
  • Hi, I don't think this could be an alternative since the id format is choosen by the customer. This method wouldn't work if a customer chooses an id like "A/B/C/D/E/F". – alFadorMX Oct 27 '15 at 10:38
  • You made it sound like you were in control of the api. If that is the case then the customer does not have control of the routes. If they needs to pass information on the route you would know what it is. You would have to or you would not know what parameters to expect. – Stephen Brickner Oct 27 '15 at 11:12