Search code examples
c#asp.netasp.net-web-apiodata

How to create a Register User action in OData asp.net Web API


I am trying to implement a method for registering a user in my OData based asp.net Web API. The ploblem is i would like to have it on the root url followed by /Register.

The register action itself makes use of a RegisterModel which contains the right information to create the user.

I have tried to add a custom action in the WebApiConfig. Problem with this is that OData then expects a simple parameter and sending whole models over won't succeed.

Simple action specification like below won't work for me. The endpoint can be found but the model is always 'null'.

var actionRegister = builder.Action("Register");
actionRegister.Parameter<RegisterModel>("model");
actionRegister.Returns<IdentityResult>();

The API method is started as follows:

[AllowAnonymous]
[HttpPost]
[ODataRoute("Register")]
public async Task<IHttpActionResult> Register(RegisterModel model)
{

Any help is welcome. My ultimate goal would be to have a controller call in my user controller that can register users via the route /Register.


Solution

  • Please take a look at the http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataActionsSample/ODataActionsSample/ sample code here, the unbound function should meet your needs.