我遇到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;
}
当我想将http post方法发送到&#34; / api / contactgroups&#34;出现此错误:
{
"$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});
任何人都可以帮我解决吗?
答案 0 :(得分:10)
我在这里找到了更好的解决方案: 我们可以使用前缀[RouteName(“someRouteName”)] 请参阅下面的链接,Mike wasson在Route Prefixes
中对其进行了描述希望有所帮助
答案 1 :(得分:6)
添加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;
}