Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple actions were found that match the request http post method

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?

like image 347
Hamid Avatar asked Apr 28 '14 13:04

Hamid


1 Answers

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

like image 95
Hamid Avatar answered Sep 29 '22 09:09

Hamid