I have problem that adding query string to my web api url results in "No action was found on the controller 'xxx' that matches the request".
I slightly modified the default web api project and changed DefaultApi route to include eventName route parameter:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{eventName}/values/{id}",
defaults: new { id = RouteParameter.Optional, controller = "values" }
);
This url: http://localhost:51115/api/test/values works fine
but this url: http://localhost:51115/api/test/values?param1=value1 doesn't and I get back "No action was found on the controller 'values' that matches the request."
Before modification both urls: http://localhost:51115/api/values and http://localhost:51115/api/values?param1=value1 worked as expected.
Yes, you are right. If I add the eventName as action parameter it is working. Unfortunately, that is not what I wanted. In my case I would need to add this param to all my actions and include code there that could be done in "OnActionExecuting" method. Also
this behavior is different form MVC routes where I am using this approach.
@LittleClive: Sorry, I forgot to quote your part: You are right = LittleClive is right
The main thing is that the route without query parameters is working fine and doesn't metter that there is no "eventName" parameter for that action. From my point of view something is wrong
What is missing in your sample is the eventName parameter.
public IEnumerable<string> Get(string eventName)
Actually action selector select action method(s) from {action} route parameter or infer action method(s) from the http verb, route parameter and querystring. This seems to be a
bug. Because it only occur when you have not bind a (custom) route parameter and querystring. The behaviour should be consistant.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
pvasek
Member
4 Points
6 Posts
Query string doesn't work for some routes
Apr 12, 2012 06:57 AM|LINK
I have problem that adding query string to my web api url results in "No action was found on the controller 'xxx' that matches the request".
I slightly modified the default web api project and changed DefaultApi route to include eventName route parameter:
routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{eventName}/values/{id}", defaults: new { id = RouteParameter.Optional, controller = "values" } );This url: http://localhost:51115/api/test/values works fine
but this url: http://localhost:51115/api/test/values?param1=value1 doesn't and I get back "No action was found on the controller 'values' that matches the request."
Before modification both urls: http://localhost:51115/api/values and http://localhost:51115/api/values?param1=value1 worked as expected.
Any ideas?
jdothoffman
Member
475 Points
99 Posts
Re: Query string doesn't work for some routes
Apr 17, 2012 12:21 PM|LINK
Can you post the Action method which this route is supposed to resolve to?
-Blog
LittleClive
Member
91 Points
65 Posts
Re: Query string doesn't work for some routes
Apr 17, 2012 01:43 PM|LINK
Wouldn't it be expecting "eventName" to be a parameter on the action method?
pvasek
Member
4 Points
6 Posts
Re: Query string doesn't work for some routes
Apr 18, 2012 09:09 AM|LINK
I am using this action:
public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; }pvasek
Member
4 Points
6 Posts
Re: Query string doesn't work for some routes
Apr 18, 2012 09:18 AM|LINK
Yes, you are right. If I add the eventName as action parameter it is working. Unfortunately, that is not what I wanted. In my case I would need to add this param to all my actions and include code there that could be done in "OnActionExecuting" method. Also this behavior is different form MVC routes where I am using this approach.
pvasek
Member
4 Points
6 Posts
Re: Query string doesn't work for some routes
Apr 18, 2012 09:30 AM|LINK
@LittleClive: Sorry, I forgot to quote your part: You are right = LittleClive is right
The main thing is that the route without query parameters is working fine and doesn't metter that there is no "eventName" parameter for that action. From my point of view something is wrong
I created an issue on aspnetwebstack (http://aspnetwebstack.codeplex.com/workitem/60?ProjectName=aspnetwebstack) but no answer yet.
You can find there also complete example project which tries to describe these things in detail.
LittleClive
Member
91 Points
65 Posts
Re: Query string doesn't work for some routes
Apr 18, 2012 09:36 AM|LINK
You may be able to remove it using something like:
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Query string doesn't work for some routes
Apr 22, 2012 09:46 AM|LINK
What is missing in your sample is the eventName parameter.
Actually action selector select action method(s) from {action} route parameter or infer action method(s) from the http verb, route parameter and querystring. This seems to be a bug. Because it only occur when you have not bind a (custom) route parameter and querystring. The behaviour should be consistant.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD