5

I have the following code

[HttpGet] 
[Route("publish/{id}")] 
public IHttpActionResult B(string id, string publishid=null) { ... }

So as far as I understood,

~/..../publish/1?publishid=12
~/..../publish?id=1&publishid=12

Should work and bind both parameters but it won't work on the second case. In the first case, publishid will not be bound.

So I do not understand why this is not working. Any idea why it is in this way?

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Peyman
  • 3,059
  • 1
  • 33
  • 68

1 Answers1

5

The second case will not work because id is a required variable in the route template publish/{id}. In Web API first route template matching happens and then the action selection process.

other cases:

  1. publish/1 - will not work as action B is saying that publishid is required. To prevent this you can change the signature of action to be something like B(string id, string publishid=null) and only id is bound
  2. publish/1?publishid=10 - works as expected where both are bound.
Kiran
  • 56,921
  • 15
  • 176
  • 161
  • Thanks, I actually I am doing "string publishid=null" I just forgot to put it in my question. I edited the code in the question. – Peyman Dec 30 '13 at 19:39
  • 1
    How about ~/..../publish?id=1&publishid=2 I think that is also valid but still is not working for me. it will give me 404 Not Found ! – Peyman Dec 30 '13 at 19:44
  • Actually by `second case` i meant this scenario...This will not work because `id` is a required variable in the route template `publish/{id}`. In Web API first route template matching happens and then the action selection process... – Kiran Dec 30 '13 at 21:14