4
votes

I'm looking to create an unbound action on a WebApi 2.2 OData 4 service, but can't figure out how to set it up correctly.

Here's my stripped down code (in a controller called UsersController):

[HttpPost]
public IHttpActionResult InitializeUser([FromODataUri] int key, ODataActionParameters parameters)
{
    // code to save user to DB & initialize account information...
    return Ok<User>(new User());
}

And my WebApiConfig method:

builder.Action("RegisterNewUser").ReturnsFromEntitySet<User>("Users");

I get back a 404 when I call this method in fiddler {"Message":"No HTTP resource was found that matches the request URI 'http://localhost/RegisterNewUser'."}.

The odata service works fine, and supports all the normal CRUD verbs.

1

1 Answers

3
votes

You need to add [ODataRoute("RegisterNewUser")] to the InitializeUser. Looks like:

[HttpPost]
[ODataRoute("RegisterNewUser")]
public IHttpActionResult InitializeUser(ODataActionParameters parameters)
{
    // code to save user to DB & initialize account information...
    return Ok<User>(new User());
}

Note: As it is an unbound action, the parameter "[FromODataUri] int key," is not needed.

Here is an action sample, just for your reference: https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataActionsSample/.