Search code examples
asp.net-coreodata

ASP.NET Core OData Action With Two Parameters


I have this action method available to OData:

[HttpPost]
[ODataRoute("({id})/Replace")]
public Blog Replace([FromODataUri] int id, Blog blog)

This responds for POST requests on /odata/Blogs(1)/Replace. It is working except for the part that the blog parameter is never bound from the POST, that is, it is not null, but has default values for all properties. If I add [FromBody] to the parameter, it becomes null. I also tried with a parameter of type ODataActionParameters, but it is always null. This is the relevant part of my model:

var replace = builder.EntitySet<Blog>("Blogs").EntityType.Action("Replace");
//replace.Parameter<int>("id").Required();  //this can be added or not, it doesn't matter
replace.EntityParameter<Blog>("blog").Required();
replace.Returns<int>();

I read somewhere that an OData action cannot have two parameters, but I am not sure of that. I need the id parameter to be present, so I need to find a solution for this. I am using ASP.NET Core 3.1 and the latest versions of all packages. What am I doing wrong?


Solution

  • The solution turned out to be simple:

    1. The parameter declaration was wrong: the name of the entityset is “blogs”, not “blog”
    2. I was posting the JSON for the “Blog” entity, but I had to modify it so as to be included in a parameter “blog”, as this:

      { “blog”: { “BlogId”: 1, << rest of the Blog properties >> } }

    This solved my problem.