加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在控制器操作中访问$orderBy等查询参数?

发布时间:2020-12-15 08:37:12 所属栏目:百科 来源:网络整理
导读:我只是阅读Microsoft REST API Guidlines( https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md),并且有一个以美??元符号开头的查询参数,例如: $排序依据. 9.6 Sorting collections The results of a collection query MAY be sorted b
我只是阅读Microsoft REST API Guidlines( https://github.com/Microsoft/api-guidelines/blob/master/Guidelines.md),并且有一个以美??元符号开头的查询参数,例如: $排序依据.

9.6 Sorting collections

The results of a collection query MAY be sorted based on property
values. The property is determined by the value of the $orderBy query
parameter.

现在,如果我尝试在操作方法中定义类似$orderBy的方法参数,那么它在语法上是不正确的($orderBy不是有效的标识符).

public class ExampleController : Controller
{
    // this is syntactically not correct
    public IActionResult Collection(....,string $orderBy = null)
    {
         ...
    }
}

如何在ASP.NET Core的操作方法中访问以美元符号开头的查询参数?

解决方法

使用FromQuery并设置名称[FromQuery(Name =“$orderBy”)] string orderBy:
public class ExampleController : Controller
{        
    public IActionResult Collection(....,[FromQuery(Name = "$orderBy")]string orderBy = null)
    {
         ...
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读