【问题标题】:Web api method not being hit when passing two parameters传递两个参数时未命中 Web api 方法
【发布时间】:2019-09-27 11:39:52
【问题描述】:

我已经编写了接受两个参数的 asp.net web.api 方法。当我从邮递员调用该方法时,它不会命中 web 方法。当我定义接受一个参数并用一个参数调用的方法时它工作正常,但当我用两个参数定义它并用两个参数调用时它不调用

    [HttpGet]
    [Route("api/terms/{id}/{invested}")]
    public IHttpActionResult Details(int id, bool invested)
    {

        var viewModel = GetTermsViewModel(id, invested);
        if (viewModel == null) return NotFound();
        return Ok(viewModel);
    }

【问题讨论】:

标签: asp.net-web-api


【解决方案1】:

您无需在传入的请求 URL 中指定 /details/
您应该调用的 URL 是

/api/terms/5508/true

从评论编辑:

要启用属性路由,请转到 WebApiConfig 类中的 Register 方法并添加以下内容:

config.MapHttpAttributeRoutes();

了解attribute routing here

【讨论】:

  • 我收到消息 MessageDetail": "在控制器 'Terms' 上找不到与名称 '5508' 匹配的操作。
  • 正如我之前提到的,如果我只是将参数设置为 id 并使用 id 值调用它,该方法就会命中。本地主机:56888/api/terms/details/5508
  • 这听起来像是您正在尝试使用属性路由,但可能还没有启用它,传统的约定路由正在发挥作用。
  • 可能是。不知道如何解决这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多