Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top