找到符合请求的多个操作:***

时间:2018-05-24 21:23:02

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

我正试图从邮递员那里调用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();
    }
}

APIController:

 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这应该足够但它不起作用。 我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

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

最简单的方法是使用属性路由。 E.g。

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`
    }
}

答案 1 :(得分:0)

解决方案1:

我在WebApiConfig类中添加了Route Config

 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将起作用,但它需要一个查询字符串参数,其中sollution 2也适用于后期参数(FormBody)

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

快乐编码