Multiple actions were found that match the request http post method

15,905

Solution 1

I found a better solution here: we can use prefix [RouteName("someRouteName")] please see link below that Mike wasson describes it in Route Prefixes

hope it helps

Solution 2

Adding ActionName attribute will resolve the problem.

// POST api/contactgroups/all
    [System.Web.Http.ActionName("All")]
    public IEnumerable<ContactsGroup> All()
    {
        return _biz.GetAllContactsGroup();
    }

    // POST api/contactgroups/find/5
    [System.Web.Http.ActionName("Find")]
    public ContactsGroup FindById(int id)
    {
        return _biz.GetContactGroup(id);
    }

    // POST api/contactgroups/create
    [System.Web.Http.ActionName("Create")]
    public HttpResponseMessage CreateContactGroup(ContactsGroup item)
    {
        item.UserId = HttpContext.Current.User.Identity.GetUserId();
        item.DateAdded = DateTime.Now;

        if (!ModelState.IsValid)
            return Request.CreateResponse(HttpStatusCode.NotAcceptable, item);

        if (!_biz.CreateContactGroup(item)) return Request.CreateResponse(HttpStatusCode.NotFound, item);

        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, item);
        response.Headers.Location = new Uri(Request.RequestUri + item.Id.ToString());
        return response;
    }
Share:
15,905
Hamid
Author by

Hamid

I'm a web developer at kendez group in Iran. I develop web apps using Microsoft and Google technologies... I would love to change the world, but they won't give me the source code

Updated on June 26, 2022

Comments

  • Hamid
    Hamid almost 2 years

    I have a problem with Web Api 2. I have wrote these 3 actions in my ContactGroupsController web api.

    // POST api/contactgroups
    [System.Web.Http.HttpPost]
    public IEnumerable<ContactsGroup> All()
    {
        return _biz.GetAllContactsGroup();
    }
    
    
    
    
    // POST api/contactgroups/5
    [System.Web.Http.HttpPost]
    public ContactsGroup FindById(int id)
    {
        return _biz.GetContactGroup(id);
    }
    
    
    
    
    // POST api/contactgroups
    public HttpResponseMessage CreateContactGroup(ContactsGroup item)
    {
        item.UserId = HttpContext.Current.User.Identity.GetUserId();
        item.DateAdded = DateTime.Now;
    
        if (!ModelState.IsValid)
            return Request.CreateResponse(HttpStatusCode.NotAcceptable, item);
    
        if (!_biz.CreateContactGroup(item)) return Request.CreateResponse(HttpStatusCode.NotFound, item);
    
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, item);
        response.Headers.Location = new Uri(Request.RequestUri + item.Id.ToString());
        return response;
    }
    

    when i want to send an http post method to "/api/contactgroups" this error appears:

      {
        "$id": "1",
        "Message": "An error has occurred.",
        "ExceptionMessage": "Multiple actions were found that match the request: \r\nSystem.Collections.Generic.IEnumerable`1[NikSms.Models.ContactsGroup] All() on type NikSms.Web.Api.ContactGroupsController\r\nSystem.Net.Http.HttpResponseMessage CreateContactGroup(NikSms.Models.ContactsGroup) on type NikSms.Web.Api.ContactGroupsController",
        "ExceptionType": "System.InvalidOperationException",
        "StackTrace": "   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n   at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"
    }
    

    this is WebApiConfig.cs contents:

    config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                                         "DefaultApiCtrl",
                                         "api/{controller}/{action}/{id}",
                                         new { id = RouteParameter.Optional});
    
            config.Routes.MapHttpRoute(
                                        "DefaultApi",
                                        "api/{controller}/{id}",
                                        new { id = RouteParameter.Optional});
    

    can anyone help me to solve it?

  • StinkyCat
    StinkyCat almost 10 years
    You didn't just add [ActionName] in your answer, you replaced the [HttpPost]. At least for me, I had to keep both, for the solution to work. Anyway thks for the tip!