Question

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?

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top