多次到多次的crud请求

时间:2017-05-31 20:10:52

标签: c# asp.net-core .net-core

所以一个典型的问题就是这样。

[Route("api/[controller]")]
public class ProvidersController : Controller {

    [HttpGet]
    public IEnumerable<object> Get(){}

    [HttpGet("{id}")]
    public IActionResult Get(int id) {}

    // POST api/provider
    [HttpPost]
    public IActionResult Post([FromBody]Provider value){}

    // PUT api/provider/5
    [HttpPut("{id}")]
    public IActionResult Put(int id, [FromBody]Provider value){}

    // DELETE api/provider/5
    [HttpDelete("{id}")]
    public IActionResult Delete(int id) {}
}

但我想将它扩展为多对多类型的资源。

我有/ api / provider / {id} / resolution [GET,POST,PUT,DELETE]等路径。

我是在另一个班级做这个还是扩展这个?我可以把它添加到这个班级吗?

我尝试做类似下面的事情,但似乎不应该在同一个班级。

[Route("api/[controller]")]
public class ProvidersController : Controller {

    [HttpGet]
    public IEnumerable<object> Get(){}

    [HttpGet("{id}")]
    public IActionResult Get(int id) {}

    // POST api/provider
    [HttpPost]
    public IActionResult Post([FromBody]Provider value){}

    // PUT api/provider/5
    [HttpPut("{id}")]
    public IActionResult Put(int id, [FromBody]Provider value){}

    // DELETE api/provider/5
    [HttpDelete("{id}")]
    public IActionResult Delete(int id) {}

    Route["{providerId}/resources/"]
    public IEnumerable<object> Get(int providerId){}

    Route["{providerId}/resources/{id}"]
    public IActionResult<object> Get(int providerId, int id){}
}

1 个答案:

答案 0 :(得分:1)

我没有看到任何理由将其移动到另一个控制器。当然你可以移动,但总会有这两个get方法(Get(int providerId)和Get(int providerId,int id))