【发布时间】:2017-07-08 16:44:46
【问题描述】:
我正在使用带有可选查询字符串参数的 REST 动词来开发 ASP.Net Core Web API。
例如,API 调用如下所示;
http://localhost:5010/servicetypecode?stc=123
或 http://localhost:5010/servicetypecode?mailclasscode=fc
我正在使用这样的代码来设置查询字符串
public IActionResult Get([FromQuery] string mailclasscode, [FromQuery] string stc) { ... }
因此,在我的方法主体中,我正在像这样单独评估每个字符串...
if (!string.IsNullOrEmpty(mailclasscode)) { ... }
if (!string.IsNullOrEmpty(stc)) { ... }
但是,我想允许我的用户直接对 API 进行 GET,而不提供查询字符串参数,并返回所有记录的列表,未经过滤。
所以这样的电话;
http://localhost:5010/servicetypecode
但我不希望在我的方法体中这样做,特别是如果我有很多查询字符串参数;
if (string.IsNullOrEmpty(mailclasscode) && string.IsNullOrEmpty(stc)) { ... }
有没有一种方法可以简单地确定是否没有提供查询字符串参数而不评估每个可能的查询字符串参数?
另外,在我要传递许多不同的查询字符串参数的情况下,有没有比测试每个可能的参数更好的方法来评估查询字符串参数?
提前感谢您提供的任何帮助。
【问题讨论】:
标签: httprequest query-string asp.net-core-webapi request.querystring querystringparameter