【问题标题】:web api multiple post methods with different parameter type具有不同参数类型的web api多个post方法
【发布时间】:2016-11-25 08:35:26
【问题描述】:

我可以有 2 个不同数据类型的 post 方法,如下所示:-

[HttpPost]        
public HttpResponseMessage Post([StringBody]string data)
{
   // Logic
}

[HttpPost]
public HttpResponseMessage Post(Requirements objRequirement)
{ 
  //Logic
}

我在邮递员中遇到错误:-

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>Multiple actions were found that match the request: 
System.Net.Http.HttpResponseMessage Post(Newtonsoft.Json.Linq.JObject) on type ATL.Trebuchet.RestApi.Controllers.ValuesController
System.Net.Http.HttpResponseMessage Post(ATL.Trebuchet.RestApi.Models.Requirements) on type ATL.Trebuchet.RestApi.Controllers.ValuesController</ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace>   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)</StackTrace>
</Error>

请帮助如何使用具有不同类型参数的相同 Post 方法

我以Text (text/plain)发送数据

编辑 1:

【问题讨论】:

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


    【解决方案1】:

    如果你想在同一个控制器中使用多个 post 方法,你需要将它们映射到不同的路由或动作

    对于Web api 1

    将路由添加到 WebApiConfig,您可以查看 answer 了解详细信息,但重要的是指定默认路由 api/controller/id 仅接受整数。否则操作将被视为字符串 ID。

    routes.MapHttpRoute(
        name: "ControllerAndId",
        routeTemplate: "api/{controller}/{id}",
        defaults: null,
        constraints: new { id = @"^\d+$" } // Only integers 
    );
    routes.MapHttpRoute(
        name: "ActionApi",
        routeTemplate: "api/{controller}/{action}"
    );
    

    然后在控制器中指定方法之上的动作

    public class DataController : ApiController
    {
        [HttpPost]  
        [ActionName("post1")]      
        public HttpResponseMessage Post([StringBody]string data)
        {
           // Logic
        }
    
        [HttpPost]
        [ActionName("post2")]
        public HttpResponseMessage Post(Requirements objRequirement)
        { 
          //Logic
        }
    }
    

    对于Web api 2

    这里可以使用属性路由

    [RoutePrefix("api/data")]
    public class DataController : ApiController
    {
        [HttpPost]  
        [Route("post1")]      
        public HttpResponseMessage Post([StringBody]string data)
        {
           // Logic
        }
    
        [HttpPost]
        [Route("post2")]
        public HttpResponseMessage Post(Requirements objRequirement)
        { 
          //Logic
        }
    }
    

    第一个 post 方法将被称为 api/data/post1 和第二个 api/data/post2

    【讨论】:

    • 不支持 routePrefix .. 它告诉我添加新类.. 见 edit1
    • 为 Route 生成类:[Route("")]
    • 抱歉,没看到您使用的是 web api 1。在 web api 2 中,您可以使用我的解决方案所基于的属性路由
    • 有没有针对Web api 1 的解决方案??
    • 与 url 相同的错误 :- localhost:57578/api/Values/PostString 注意:- PostString :- ActionName > [ActionName("PostString")]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多