0
votes

I have a OData Controller. Delete and Patch methods are working great, however I cannot hit the Post method. Do I have to post the whole object or is posting just the key ok?

The Patch request that works well:

PATCH http://localhost:50161/odata/Carousel(3)

The Post request that returns 404:

POST http://localhost:50161/odata/Carousel(3)

I am not sending any body with the post request

Controller:

public class CarouselController : ODataController<CarouselTableDto>
{
    private readonly CarouselService _service;

    public CarouselController(CarouselService service)
    {
        _service = service;
    }

    public class ModelRegistration : ModelRegistration<CarouselTableDto> { }

    protected override IQueryable<CarouselTableDto> GetData()
    {
        return _service.QueryAll();
    }

    public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<CarouselTableDto> data)
    {
        ChangePassivable(data, active => _service.SetIsActive(key, active));
        return StatusCode(HttpStatusCode.NoContent);

    }

    public async Task<IHttpActionResult> Delete([FromODataUri] int key)
    {
        _service.Delete(key);
        return StatusCode(HttpStatusCode.NoContent);
    }

    [HttpPost]
    public IHttpActionResult Post([FromODataUri] int key)
    {
        _service.Copy(key);
        return StatusCode(HttpStatusCode.NoContent);
    }
}
1

1 Answers

0
votes

If you want to post an entity, you should post to the entity set, like .../Carousels

and payload like:

        string payload = @"{ 
            ""ID"": 3, 
            ""Name"": ""def"" 
        }";