[英]C# - IF with more string variables and OR operator
我想检查是否有任何变量为空或 null 换句话说,我想检查是否发送了所有参数。 我有这个代码,但它不起作用。
[HttpGet]
[Route("{regid}/{year}")]
public HttpResponseMessage Get(string regid, int? year, string un, string ps)
{
var isRegIDNumeric = int.TryParse(regid, out _);
if (!isRegIDNumeric || year == null || (String.IsNullOrEmpty(un)) || (String.IsNullOrEmpty(ps)))
{
...
}
如果我这样调用,则缺少一个参数,IF 不起作用
localhost/test/?regid=001&year=2019&un=ws.test
我收到了这条消息:
<Error>
<Message>No HTTP resource was found that matches the request URI
'localhost/test/?regid=001&year=2019&un=ws.test'.
</Message>
<MessageDetail>No action was found on the controller 'test' that matches
the request.
</MessageDetail>
</Error>
如果我使用所有参数调用一切正常,则执行整个过程。
localhost/test/?regid=001&year=2019&un=ws.test&ps=test
当我只有 2 个变量时......
[HttpGet]
[Route("{regid}/{year}")]
public HttpResponseMessage Get(string regid, int? year)
{
var isRegIDNumeric = int.TryParse(regid, out _);
if (!isRegIDNumeric || year == null)
{
...
}
并像这样调用,缺少一个参数......
localhost/test/?regid=001
一切正常,如果定期执行。
如果其中任何一个为空或为空,如何使所有 4 个变量都具有功能性 IF?
在您的 4 个参数示例中,两个字符串参数都是必需的。 这就是为什么你会得到一个错误。
如果您希望它们是可选的,请为它们分配默认值 null。 比 if 语句会起作用。
或者添加另一个 Get() 方法。
我相信这个另一个问题有你的答案,并证实了David Pivotar所说的话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.