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?

4
  • 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 } ); Commented Oct 26, 2015 at 9:45
  • Please post your action method signature. Also you mean "X/Y/Z" together is the string passed to id parameter of your action? Commented Oct 26, 2015 at 9:48
  • 1
    My understanding is you have '/' in you id.. It is not wise to have it as data in your urls.. read this: stackoverflow.com/questions/3235219/… Commented Oct 26, 2015 at 9: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. Commented Oct 27, 2015 at 10:34

2 Answers 2

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 } ); 
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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".
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.