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?

1 Answer 1

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

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.