【问题标题】:Multiple actions were found that match the request:***找到多个与请求匹配的操作:***
【发布时间】:2018-11-04 05:06:23
【问题描述】:

我正在尝试从邮递员调用 WEBAPI。我的 AI 方法是 post,但是当我执行它时,我得到以下给定错误

找到多个与请求匹配的操作:***

以下是我的代码: webapi route.config

 public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
           name: "Api Default",
           routeTemplate: "api/{controller}/{method}/{id}",
           defaults: new { id = RouteParameter.Optional }
       );
        //config.Routes.MapHttpRoute(
        //    name: "DefaultApi",
        //    routeTemplate: "api/{controller}/{id}",
        //    defaults: new { id = RouteParameter.Optional }
        //);

        // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
        // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
        // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
        config.EnableQuerySupport();

        // To disable tracing in your application, please comment out or remove the following line of code
        // For more information, refer to: http://www.asp.net/web-api
        config.EnableSystemDiagnosticsTracing();
    }
}

API控制器:

 public class MembershipController : ApiController
    {
      [System.Web.Http.ActionName("Upload")]
        public HttpResponseMessage Upload(string employeedetails)
        {
             `Some Code`
         }

      [System.Web.Http.ActionName("BulkUpload")]
 [System.Web.Http.HttpPost]
        public HttpResponseMessage BulkUpload(string employeedetails)
        {
                `Some Code`
         }
            [System.Web.Http.ActionName("Transfer")]
            public HttpResponseMessage Transfer(string employeedetails)
              {
                      `Some Code`
               }

         }

我没有得到什么是错误的方法具有不同的操作名称,并且路由配置也是完全限定的 api url,其中包含控制器方法和 id 作为可选参数。要识别 url,这应该足够了,但它不起作用。 我错过了什么吗?

【问题讨论】:

  • 这方面也有很多东西> stackoverflow.com/questions/14534167/…
  • 所以{method} 应该是{action} @RobA?
  • @Pavan this 工作还是你还有问题?
  • @win 这个问题还没有解决。但是我观察到的是当我在查询字符串中传递数据时它可以工作,但不幸的是查询字符串有大小限制,我的数据不止于此。当我使用httpcontext.Current.Request.Form.GetValue(0);我以字符串格式获取值,但我希望它在方法参数中,而不是在方法中编写代码来获取它。如何做到这一点?

标签: c# .net asp.net-web-api2


【解决方案1】:

如果这是 RESTful API,您不能拥有三个 HttpPost,除非您通过 URL slug 区分它们。

最简单的方法是使用属性路由。例如

public class MembershipController : ApiController
{
    // POST api/Membership/data/Upload
    [Route("api/Membership/{employeedetails}/Upload")]
    [HttpPost]
    public HttpResponseMessage Upload(string employeedetails)
    {
        `Some Code`
    }

    // POST api/Membership/data/Bulk
    [Route("api/Membership/{employeedetails}/Bulk")]
    [HttpPost]
    public HttpResponseMessage BulkUpload(string employeedetails)
    {
        `Some Code`
    }

    // POST api/Membership/data/Transfer
    [Route("api/Membership/{employeedetails}/Transfer")]
    [HttpPost]
    public HttpResponseMessage Transfer(string employeedetails)
    {
        `Some Code`
    }
}

【讨论】:

  • 这是可能的解决方案之一。
【解决方案2】:

解决方案 1:

我在 WebApiConfig 类中添加了路由配置

 public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {

   config.Routes.MapHttpRoute(
       name: "Membership",
       routeTemplate: "api/{controller}/{method}/{id}",
       defaults: new { id = RouteParameter.Optional }
   );
    config.Routes.MapHttpRoute(
       name: "Api Default",
       routeTemplate: "api/{controller}/{method}/{id}",
       defaults: new { id = RouteParameter.Optional }
   );
    //config.Routes.MapHttpRoute(
    //    name: "DefaultApi",
    //    routeTemplate: "api/{controller}/{id}",
    //    defaults: new { id = RouteParameter.Optional }
    //);

    // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
    // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
    // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
    config.EnableQuerySupport();

    // To disable tracing in your application, please comment out or remove the following line of code
    // For more information, refer to: http://www.asp.net/web-api
    config.EnableSystemDiagnosticsTracing();
}

}

解决方案 2

public class MembershipController : ApiController
{
  [System.Web.Http.ActionName("Upload")]
    public HttpResponseMessage Upload([FromBody]string employeedetails)
    {
         `Some Code`
     }
[System.Web.Http.HttpRoute("api/membeship/BulkUpload")]
 [System.Web.Http.HttpPost]
        public HttpResponseMessage BulkUpload(string employeedetails)
        {
                `Some Code`
         }
[System.Web.Http.HttpRoute("api/membeship/Transfer")]
            public HttpResponseMessage Transfer([FromBody]string employeedetails)
              {
                      `Some Code`
               }

         }

如果我们比较解决方案 1 和解决方案 2,那么解决方案 1 将起作用,但它需要一个查询字符串参数,其中解决方案 2 也适用于后参数(FormBody)

我正在详细研究解决方案 2 的不同之处。因为当我们从解决方案 2 中删除 HTTPRoute 时,它​​也只需要查询字符串参数,如果我们尝试使用 post 传递参数,那么它将作为空值传递。很快我就会发现它并分享关于堆栈溢出的详细分析。

快乐编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2017-10-26
    • 2016-10-18
    相关资源
    最近更新 更多