未找到OData控制器上的Post方法

时间:2016-07-05 13:21:49

标签: c# model-view-controller asp.net-web-api odata

我有一个OData控制器。删除和修补方法工作得很好,但我无法点击Post方法。我是否必须发布整个对象或仅发布密钥好吗?

运行良好的补丁请求:

  

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

返回404的Post请求:

  

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

我没有发送任何带有帖子请求的正文

控制器:

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 个答案:

答案 0 :(得分:0)

如果您想发布实体,您应该发布到实体集,例如... / Carousels

和有效负载如:

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