Sunday, January 20, 2019

How may I have two actions with the same verb in same controller in a .NET Core application?

You need to differentiate the two as otherwise you just get a confusing CORS error when you try the verb. The differentiation needs to be route based:

[Route("api/President")]
public class PresidentController : Controller
{
   public IFlatFileMechanics _flatFileMechanics;
   
   [HttpPut]
   public ObjectResult Put([FromBody] List<President> presidents) {
      _flatFileMechanics.SetPresidents(presidents);
      return Ok(null);
   }
   
   [HttpPut("{id}")]
   public void Put(string id, [FromBody] President president)
   {

 
 

What you cannot do is method overloading. I tried that and there probably isn't any way for .NET Core to know what's in a JSON object proactively in advance of you landing at an API endpoint.

No comments:

Post a Comment