Multiple actions were found that match the request Web API?

后端 未结 8 1198
再見小時候
再見小時候 2020-12-09 08:09

I am using web API and i am new in this. I am stuck in a routing problem. I have a controller with following actions :

    // GET api/Ceremony
    public IEn         


        
8条回答
  •  一整个雨季
    2020-12-09 09:09

    Luckily nowadays with WEB API2 you can use Attribute Routing. Microsoft has gone open source on a big scale and then a wizard named Tim McCall contributed it from the community. So since somewhere end 2013, early 2014 you can add attributes like [Route("myroute")] on your WEB API methods. See below code example.

    Still - as I just found out - you have to make sure to use System.Web.Http.Route and NOT System.Web.Mvc.Route. Otherwise you'll still get the error message Multiple actions were found that match the request.

    using System.Web.Http;
    ...
    
    [Route("getceremonies")]
    [HttpGet]
    // GET api/Ceremony
    public IEnumerable GetCeremonies()
    {
        return db.Ceremonies.AsEnumerable();
    }
    
    [Route("getceremony")]
    [HttpGet]
    // GET api/Ceremony/5
    public Ceremony GetCeremony(int id)
    {
        Ceremony ceremony = db.Ceremonies.Find(id);
        return ceremony;
    }
    
    [Route("getfilteredceremonies")]
    [HttpGet]
    public IEnumerable GetFilteredCeremonies(Search filter)
    {
        return filter.Ceremonies();
    }
    

提交回复
热议问题