【问题标题】:Several actions found Web Api [duplicate]找到了几个操作 Web Api [重复]
【发布时间】:2018-12-28 14:49:11
【问题描述】:

我有 web api 2.0 项目 当我尝试用相同的参数实现几个方法时,我有这个错误:找到了几个动作

namespace WebApi.Controllers
{
    public class EventController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage GetTags(string token, int messageId)
        {
            return ApiCall<List<EntityTag>>.CallApi(token, ServicesMessage.GetTags(messageId));
        }

        [HttpGet]
        public HttpResponseMessage Get(string token, int eventId)
        {
            return ApiCall<EntityEvent>.CallApi(token, ServicesEvent.Get(eventId));
        }
    }
}

有什么想法吗?

谢谢

【问题讨论】:

  • 您的 startup.cs 文件看起来如何?
  • 你应该指定路线或者你应该在你的死记硬背中添加动作名称。模板应该是这样的: routeTemplate: "api/{controller}/{action}/{id}"

标签: c# webapi2


【解决方案1】:

在你的webapiconfig.cs写下这一行

  config.Routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });

你的控制器:

namespace WebApi.Controllers
{
    public class EventController : ApiController
    {
        [HttpGet]
        public HttpResponseMessage Tags(string token, int messageId)
        {
            return ApiCall<List<EntityTag>>.CallApi(token, ServicesMessage.GetTags(messageId));
        }

        [HttpGet]
        public HttpResponseMessage Events(string token, int eventId)
        {
            return ApiCall<EntityEvent>.CallApi(token, ServicesEvent.Get(eventId));
        }
    }
}

使用它,您可以为每个获取请求指定操作名称。

发布

当你想用 post 方法做同样的事情时,只需在weapiconfig.cs 中写下一行

config.Routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });

然后你可以给一个动作名称。使用后无需路由。

当你想调用api时,写如下:

api/Event/Tags //parameter as per your requirement
api/Event/Events //parameter as per your requirement

【讨论】:

    【解决方案2】:

    WebAPI 无法区分这两种方法,因为它们具有相同的参数类型和相同的 HttpVerb,并且使用隐式路由(它们上没有路由属性)。

    我非常喜欢控制器和方法的显式路由,而不是依赖于命名约定 - 所以我会尝试向方法添加路由属性:

    public class EventController : ApiController
    {
        [HttpGet]
        [Route("gettags")]
        public HttpResponseMessage GetTags(string token, int messageId)
        {
            return ApiCall<List<EntityTag>>.CallApi(token, ServicesMessage.GetTags(messageId));
        }
    
        [HttpGet]
        [Route("get")]
        public HttpResponseMessage Get(string token, int eventId)
        {
            return ApiCall<EntityEvent>.CallApi(token, ServicesEvent.Get(eventId));
        }
    }
    

    ... 并在控制器本身上添加一个 RoutePrefix 属性,如下所示:

    [RoutePrefix("/api/Event")]
    public class EventController : ApiController
    {
    }
    

    那么您应该能够通过对这些 URL 的 GET 请求调用方法:

    /api/event/get?token=xxxxx&eventId=xxxx
    

    和

    /api/event/gettags?token=xxxxx&messageId=xxxx
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多