WebAPI错误:创建POST方法时 - 找到与请求匹配的多个操作

时间:2015-07-25 04:05:17

标签: asp.net-web-api

当我尝试使用2个“获取”方法的POST方法时,我一直收到此错误

我的WebAPI:

        [ResponseType(typeof(Organization))]
        [System.Web.Mvc.HttpPost]
        [System.Web.Http.ActionName("CreateOrganization")]
        public IHttpActionResult CreateOrganization(Organization org)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            helper.InsertOrganization(org); 

            return CreatedAtRoute("DefaultApi", new { id = org.Id }, org);
        }

        //GET: api/Products/5
        [ResponseType(typeof(Organization))]
        [System.Web.Mvc.HttpGet]
        [System.Web.Http.ActionName("FindBy")]
        public IHttpActionResult FindBy(int id)
        {
            Organization org = helper.QueryOnOrganizationBy(id);
            if (org == null)
            {
                return NotFound();
            }

            return Ok(org);
        }

        [ResponseType(typeof(Organization))]
        [System.Web.Http.ActionName("All")]
        [System.Web.Mvc.HttpGet]
        public IHttpActionResult All()
        {    
            List<Organization> orgList = helper.QueryAllOnOrganization(); 

            if (orgList == null)
            {
                return NotFound();
            }

            return Ok(orgList);
          }

ERROR:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Multiple actions were found that match the request: \r\nCreateOrganization on type ProductReviews.Controllers.OrganizationsController\r\nAll on type ProductReviews.Controllers.OrganizationsController\r\nQueryAllOnTable on type ProductReviews.Controllers.OrganizationsController\r\nOpenConn on type ProductReviews.Controllers.OrganizationsController\r\nCloseConn on type ProductReviews.Controllers.OrganizationsController",
    "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.<SendAsync>d__1.MoveNext()"
}

WEB API路线:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            //To produce JSON format add this line of code
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }
    }

1 个答案:

答案 0 :(得分:1)

如果您想在控制器操作上使用[ActionName]属性,那么您的路由模板需要包含{action}

routeTemplate: "api/{controller}/{action}/{id}",

默认模板"api/{controller}/{id}"假设您只使用HTTP方法(例如,GET或POST)和可选id来区分操作。

此外,在Web API中,您应该使用HttpGet命名空间中的HttpPostSystem.Web.Http属性,而不是System.Web.Mvc