【问题标题】:How to make UrlParameter.Optional {id} to accept blank values, and still work in WEB API如何使 UrlParameter.Optional {id} 接受空白值,并且仍然可以在 WEB API 中工作
【发布时间】:2016-10-07 04:23:36
【问题描述】:

我有一个默认路由,

Default Routing: http://localhost:4709/v1/{controller}/{id}

我正在尝试创建一个新路由

My Routing: http://localhost:4709/v1/{controller}/{id}/orders

我的 Action 可以接受空值的 id

[HttpGet]
public TotalOrders Get(string id)
{
  //return
}

如何实现这一点,每当我这样做时,我都会在与请求匹配的控制器“KD”上找不到任何操作。

【问题讨论】:

  • 您要发送的请求是什么?
  • 它是一个带或不带 id 的 Get 请求。
  • 您能否提供您尝试发送的请求网址的示例
  • 还展示了如何配置路由。添加它们的顺序很重要。

标签: c# rest asp.net-web-api get routing


【解决方案1】:

你可以这样定义它,http://localhost:4709/v1/{controller}/{*others}

“*”允许多个“/”。 喜欢http://localhost:4709/v1/controller/5/orders。 所以其他人将是一串 5/订单。您将不得不编写自己的小解析器。 (空检查,拆分'/'等) 其他方式是不可能的,因为可选参数总是必须在末尾。

【讨论】:

    猜你喜欢
    • 2015-02-18
    • 2018-01-13
    • 2014-04-20
    • 2021-08-02
    • 2018-01-26
    • 2022-01-24
    • 2011-10-22
    • 2015-07-02
    • 1970-01-01
    相关资源
    最近更新 更多