1

I have a .NET Core 5 project (Rest API) that includes a controller (TestController) with an endpoint (AddOrUpdate). The signature of the endpoint looks like this:

[HttpPost, HttpPut]
public virtual IActionResult AddOrUpdate(params TDatabaseModel[] entities).

By itself that one works, but now I always have to specify a list of TDatabaseModel's in the JSON. Now I wanted to create another endpoint, which accepts only one TDatabaseModel, to prevent the creation of a list. The signature looks like this:

[HttpPost, HttpPut]
public virtual IActionResult AddOrUpdate(TDatabaseModel entity)

The problem now is that I get an exception as soon as I specify a single object (not a list) in the JSON body. The exception refers to the fact that the endpoints are "ambigous". I have also tried to make a list of TDatabaseModel's out of the endpoint with "params", but even then it remains ambiguous.

My question: is there a way to pass both a single object and a list of objects WITHOUT using different endpoint routes? So I don't want to end up using different routes (HttpPost:{{URL}}/single && HttpPost:{{URL}}/multiple). Is there any way to do that?

2
  • params doesn't make sense there as the serializer will give you an array by default. Have you tried removing that? Commented Apr 24, 2022 at 13:25
  • I see your point, but no, is still seen as ambiguous, unfortunately. Commented Apr 24, 2022 at 13:28

1 Answer 1

1

No, there is no way, either use always an array or use two endpoints.

Unfortunately, REST does not really have a representation for a batch operation (such as creating/updating multiple entities at once), so which option to go with is entirely opinionated.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, I was afraid of that already. Well, it was worth a try. Thanks for the answer.
@CaptTaifun Glad to help. I'd suggest you to discuss with consumers of the API before choosing the approach, if this is at all possible. Some people might prefer a separation (explicitness), others might prefer the simplicity of always using the same payload
Thanks for the tip. In that case I write the consumer of the API, that's why it is not that bad. Would be nicer if you could have split it up without using different routes, of course, but good. For now I always use a list, let's see how that turns out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.