0

I want to expose 2 new endpoints on my controller. 1 has the method signature to add a single entity:

[HttpPost]
[Route("v1/entities")]
public IHttpActionResult AddEntity([FromBody] Entity entity)

The other is almost identical except it accepts an IEnumerable to add multiple entities:

[HttpPost]
[Route("v1/entities")]
public IHttpActionResult AddEntities([FromBody] IEnumerable<Entity> entities)

Clearly this won't work since it won't know what action method to call. So how can I get around this to support both methods. Should I simply rename the second route to something like "v1/entitieslist"?

Thanks

2 Answers2

0

The simplest would be to change the first one to v1/entity as you're not dealing with more than one. There's an interesting discussion here on 'pure' REST and being idempotent when doing this sort of thing. I tend to not use bulk POSTing, preferring to POST one at a time.

codebrane
  • 4,290
  • 2
  • 18
  • 27
0

I would't worry about pure REST in your situation. If you have a good reason to create multiple entities in one call then do it.

You have a few options:

  1. You could create just one endpoint which takes a List of objects. Your list could have one element as well, so this covers both scenarios.

  2. You could create separate endpoints and you already know how to do that

The biggest challenge will be to decide what you want to return. In a POST I prefer to return the unique identifier of the created resource, but when you have a bulk create, some might work, some might fail, so you need to decide what will happen and make sure you cover all possible scenarios.

Ar your entities related? do you want to create entities in a transaction kind of way and roll back the first time you encounter an issue? These are questions you will need to answer

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32