Multiple Get actions with different attribute routing?

时光怂恿深爱的人放手 提交于 2019-12-24 07:39:48

问题


If I design my controller in such a way:

public class ItemController : ApiController
{
    [HttpGet]
    [RoutePrefix("item/dosomething")]
    public void DoSomething(Item item)
    { }

    [HttpGet]
    [RoutePrefix("item/dosomethingnicer")]
    public void DoSomethingNicer(Item item)
    { }

    [HttpGet]
    [RoutePrefix("item/dosomethingelse")]
    public void DoSomethingElse(Item item)
    { }
}

Would this work?


回答1:


I would expect a structure more akin to this:

[RoutePrefix("item")]
public class ItemController : ApiController
{
    [HttpGet]
    [Route("dosomething")]
    public void DoSomething(Item item)
    { }

    [HttpGet]
    [Route("dosomethingnicer")]
    public void DoSomethingNicer(Item item)
    { }

    [HttpGet]
    [Route("dosomethingelse")]
    public void DoSomethingElse(Item item)
    { }
}



回答2:


I use Web Api 2 in this way in a lot of Controllers:

[HttpGet]
[Route("~/api/{version}/research/export")]
public IHttpActionResult Export(){
     do stuff...
} 

[HttpPost]
[Route("~/api/{version}/research/list")]
public IHttpActionResult List()
{
     do stuff...
}

I use full api path description and it works with no problems.



来源:https://stackoverflow.com/questions/37435954/multiple-get-actions-with-different-attribute-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!