OData Actions with Parameters broken in latest nightlies

Topics: ASP.NET Web API
Aug 20, 2013 at 8:31 AM
Unless there has been some feature change since Mike Wasson's explanation of OData Actions (http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions), this functionality is confirmed broken in the latest nightlies.

When I define an OData Action with parameters, every request with a non-empty request body is considered to have an invalid ModelState. Even when it should be valid.

I've also tested this with both Web API 4.0.30506.0 and 5.0.0-beta2, and in those versions the actions are working correctly. However, in the nightlies 130818 and 130819 the exact same code is no longer working. For these testing purposes I've put together a small project with only the necessary code:

Code in WebApiConfig.cs
ActionConfiguration usersAction = modelBuilder.Entity<User>().Action("TestAction");
Code in UsersController.cs
public HttpResponseMessage TestAction([FromODataUri] int key, ODataActionParameters parameters)
    if (!ModelState.IsValid)
        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));

    return Request.CreateResponse(HttpStatusCode.OK, "This a return statement");
Should anyone want the source code of the entire project, please ask.
Aug 20, 2013 at 12:24 PM
Hi, Could you share a simple repro?
Aug 20, 2013 at 2:08 PM
Thanks for the quick response. Most certainly.

I've uploaded the code only project files:
https://dl.dropboxusercontent.com/u/12087295/ActionBugTester_CodeOnly.zip (4.3 MB)

And the entire solution with packages (just in case):
https://dl.dropboxusercontent.com/u/12087295/ActionBugTester.zip (29.7 MB)

If you send a POST request to /odata/Users(1)/TestAction with no request body, you get a correct 200 OK response.

However, if you send a request body with '{"IsUser": true}', you get a 406 Not Acceptable response (because of the invalid ModelState). Changing the parameter type doesn't matter, either.

It's an action on a single Entity, not the entire Collection. I haven't tested that, but I think it wouldn't matter.
Aug 20, 2013 at 2:35 PM
Thanks TristanKazu for the repro...I took a look at this...looks like the problem is because of the type of return type that you are using when defining the OData action...

Change the following code:
to say something like below:
The thing is you would want to mention the 'actual' type that you are sending back to the client, which in this case is of type 'string'. I haven't verified this with older bits but this would be the expected way to do.
Aug 20, 2013 at 6:24 PM
I have opened this bug to track this issue. https://aspnetwebstack.codeplex.com/workitem/1232
Aug 21, 2013 at 10:15 AM
Edited Aug 21, 2013 at 10:23 AM
@kichalla: Thanks for your reply. Unfortunately, in my simple project your solution seems to work indeed. However, in my own (more complex of course) actions, it does not.

On the topic of your reply, however, what should I return if I just want to return a 204 No Content (or 201 Created) response? It is not possible to return 'nothing', as far as I'm aware?

Thanks to raghuramn for making a ticket. I will keep track of it, and should it get fixed, update to that nightly and report back if it works.