I have followed this guide to create a self host Web API project, and found a strange problem that action method paramter wasn't use value in route template but instead of use value in query string.
For example, I had below route template:
config.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional } );
But if I access to http://localhost/api/Products/1?id=2 ,here I passed a same parameter as id in query string, and I got product info with id 2, it seems if have same parameter name in query
string, Web API stops use value in route template, it's just wierd, I'm not sure this is by design or have any ways to ignore this?
I just found this behavior by chance, and I tried in Web API (not self host) project, there is no such issue, the difference is the Web API controller inherits from AsyncController, but controller in self host project inherits from ApiController.
I'm a little curious about why self host Web API project has this behavior, not sure this is a bug or by design? and any way to prevent this?
webapi
0 Points
2 Posts
action parameter overwrite by query string parameter
Feb 25, 2013 10:42 AM|LINK
I have followed this guide to create a self host Web API project, and found a strange problem that action method paramter wasn't use value in route template but instead of use value in query string.
For example, I had below route template:
config.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional } );
And in ProductsController.cs I had below method:
public Product GetProductById( int id )
If i access to http://localhost/api/Products/1 I will get product info with id 1, this is looks good so far.
But if I access to http://localhost/api/Products/1?id=2 ,here I passed a same parameter as id in query string, and I got product info with id 2, it seems if have same parameter name in query string, Web API stops use value in route template, it's just wierd, I'm not sure this is by design or have any ways to ignore this?
MikeWasson
Member
499 Points
79 Posts
Microsoft
Re: action parameter overwrite by query string parameter
Feb 25, 2013 05:01 PM|LINK
You have two values that match the "id" parameter: "1" from the URI and "2" from the query string, so they are colliding.
What behavior are you trying to implement? (i.e., Why are you sending this query string value?)
- Mike
webapi
0 Points
2 Posts
Re: action parameter overwrite by query string parameter
Feb 26, 2013 01:08 AM|LINK
I just found this behavior by chance, and I tried in Web API (not self host) project, there is no such issue, the difference is the Web API controller inherits from AsyncController, but controller in self host project inherits from ApiController.
I'm a little curious about why self host Web API project has this behavior, not sure this is a bug or by design? and any way to prevent this?