Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryString with MVC 5 AttributeRouting in Web API 2

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?

like image 722
Peyman Avatar asked Dec 30 '13 05:12

Peyman


1 Answers

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.
like image 85
Kiran Avatar answered Oct 20 '22 20:10

Kiran