找到了多个与请求http post方法匹配的操作

本文关键字:方法 操作 post http 请求 找到了 | 更新日期: 2023-09-27 18:00:36

我在Web Api 2上遇到问题。我已经在我的ContactGroupsController web api中编写了这3个操作。

// 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;
}

当我想向"/api/contactgroups"发送http post方法时,会出现以下错误:

  {
    "$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()"
}

这是WebApiConfig.cs的内容:

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});

有人能帮我解决吗?

找到了多个与请求http post方法匹配的操作

我在这里找到了一个更好的解决方案:我们可以使用前缀[RouteName("someRouteName")]请参阅下面的链接,Mike wasson在Route Prefixes 中对此进行了描述

希望它能帮助

添加ActionName属性将解决此问题。

// 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;
    }