I'm having some issues with optional query parameters. Whenever I add an optional parameter to my method, it won't work anymore. I always receive 400 bad request.
// Working
public ReturnType MyMethod(int ID, int AnotherID) { .. }
// Not working
public ReturnType MyMethod(int ID, int AnotherID, bool OptionalParameter = false) { .. }
Can anyone explain me how to solve this issue? Or are optional parameters not supported for PUT/POST?
First thank you for your respond. But I don't really understand it. How is this method overloading? Maybe my example was a bit misleading. I'm not having the method twice. I was just showing how it works, and how it doesn't.
Basically I want to have an arbitrary amount of optional parameters that I can add to an URI (api/myResource?parameter1=foo¶meter2=bar). This worked fine in WCF Web API.
// Called method
public ObjectData MyMethod(int outerID, int innerID, DataObject data, bool? optionalParameter)
{
// Code
}
Just check it for null in the method and treat as false (if null). Also, just to point out, I was able to reproduce what you are seeing.. and the response I received was (via Fiddler):
<?xml version="1.0" encoding="utf-8"?><string>The parameters dictionary contains a null entry for parameter 'optional' of non-nullable type 'System.Boolean' for method 'Void Post(Int32, Int32, System.Object, Boolean)' in 'OptionalParameters.Controllers.ValuesController'.
An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.</string>
Thank you jdothoffman! This solved the issue for me.
It's surprising that having an optional parameter with a default value works in a GET method, but does not in a POST/PUT method. Having "bool optionalParameter = true" works with GET. But I better always use nullable-types for this now.
About the error message: I never got the thought that my custom media formatter (JSON.NET) hides the error message.. I better fix this, so I can see these helpful error messages too. :)
MartinJ.
Member
243 Points
80 Posts
Query Parameter with POST/PUT
Apr 16, 2012 02:01 PM|LINK
Hello.
I'm having some issues with optional query parameters. Whenever I add an optional parameter to my method, it won't work anymore. I always receive 400 bad request.
// Working public ReturnType MyMethod(int ID, int AnotherID) { .. } // Not working public ReturnType MyMethod(int ID, int AnotherID, bool OptionalParameter = false) { .. }Can anyone explain me how to solve this issue? Or are optional parameters not supported for PUT/POST?
Prashant Kum...
Star
12334 Points
1992 Posts
Re: Query Parameter with POST/PUT
Apr 16, 2012 02:07 PM|LINK
You can refer to this for a nice explanation
http://forums.asp.net/t/1162934.aspx/1
MartinJ.
Member
243 Points
80 Posts
Re: Query Parameter with POST/PUT
Apr 16, 2012 02:11 PM|LINK
First thank you for your respond. But I don't really understand it. How is this method overloading? Maybe my example was a bit misleading. I'm not having the method twice. I was just showing how it works, and how it doesn't.
Basically I want to have an arbitrary amount of optional parameters that I can add to an URI (api/myResource?parameter1=foo¶meter2=bar). This worked fine in WCF Web API.
jdothoffman
Member
475 Points
99 Posts
Re: Query Parameter with POST/PUT
Apr 16, 2012 02:21 PM|LINK
Martin, how do you have your routes declared in the Global.asax? I spun up a new project, declare the following in the Api Controller:
// POST /api/values public void Post(int id, int anotherId, bool optional = false) { }And it worked fine on POST with either url:
http://localhost:27544/api/values?id=1&anotherId=2
http://localhost:27544/api/values?id=1&anotherId=2&optional=true
-Blog
MartinJ.
Member
243 Points
80 Posts
Re: Query Parameter with POST/PUT
Apr 16, 2012 02:33 PM|LINK
For testing purpose I removed all routes and just created the one for this test.
I create it like this:
// Route adding routes.MapHttpRoute( "MyService_MethodName", "Outer/{outerID}/Inner/{innerID}", new { controller = "MyService", action = "MyMethod" }, new { httpMethod = new HttpMethodConstraint("POST") } ); // Called method public ObjectData MyMethod(int outerID, int innerID, DataObject data, bool optionalParameter = false) { // Code }When I remove the optional parameter then it works.
jdothoffman
Member
475 Points
99 Posts
Re: Query Parameter with POST/PUT
Apr 16, 2012 02:49 PM|LINK
declare the parameter as nullable..
// Called method public ObjectData MyMethod(int outerID, int innerID, DataObject data, bool? optionalParameter) { // Code }Just check it for null in the method and treat as false (if null). Also, just to point out, I was able to reproduce what you are seeing.. and the response I received was (via Fiddler):
<?xml version="1.0" encoding="utf-8"?><string>The parameters dictionary contains a null entry for parameter 'optional' of non-nullable type 'System.Boolean' for method 'Void Post(Int32, Int32, System.Object, Boolean)' in 'OptionalParameters.Controllers.ValuesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.</string>
-Blog
MartinJ.
Member
243 Points
80 Posts
Re: Query Parameter with POST/PUT
Apr 16, 2012 03:15 PM|LINK
Thank you jdothoffman! This solved the issue for me.
It's surprising that having an optional parameter with a default value works in a GET method, but does not in a POST/PUT method. Having "bool optionalParameter = true" works with GET. But I better always use nullable-types for this now.
About the error message: I never got the thought that my custom media formatter (JSON.NET) hides the error message.. I better fix this, so I can see these helpful error messages too. :)
jdothoffman
Member
475 Points
99 Posts
Re: Query Parameter with POST/PUT
Apr 16, 2012 03:21 PM|LINK
No problem, since you mention you are using a custom formatter, please beware of the issue below with Beta. I blogged about it here:
http://geekswithblogs.net/jdothoffman/archive/2012/03/06/mvc-4-beta-web-api-bug-with-binding-route-values.aspx
-Blog