0

I would like to check if any variable is empty or null in other words I would like to check if all parameters are sent. I have this code but it isn't working.

    [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 I call like this, one parameter is missing, IF isn't working

localhost/test/?regid=001&year=2019&un=ws.test

I got this message:

<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>

If I call with all parameters everything is OK, whole procedure is executed.

localhost/test/?regid=001&year=2019&un=ws.test&ps=test

When I had only 2 variables ...

[HttpGet]
[Route("{regid}/{year}")]
public HttpResponseMessage Get(string regid, int? year)
{
    var isRegIDNumeric = int.TryParse(regid, out _);
    if (!isRegIDNumeric || year == null)
    {
     ...
    }

and called like this, one parameter is missing ...

localhost/test/?regid=001

Everything was OK, IF was regularly executed.

How to make functional IF with all 4 variables if any of them is null or empty?

hoggar
  • 3,699
  • 5
  • 31
  • 41
  • 3
    You tell that the code is not working but you don't tell the input (of the case that fails) and the expected output in that case! The code seems correct so maybe your data is not what you think it is! Have you debugged it? – Phil1970 Nov 25 '19 at 01:43
  • If I omit any of parameters the IF code should be executed. Now is working if I omit regno or year but if I omit un or ps IF code is not executed – hoggar Nov 25 '19 at 01:45
  • Can you give specific values of un and ps that you think should cause the IF statement to be executed, but aren't. The code looks like it should work. – Ross Gurbutt Nov 25 '19 at 01:52
  • 1
    As written the question does contains required information for us to help you. The problem is probably not in the code that is shown. You need to show us how you call the function. – Phil1970 Nov 25 '19 at 01:57
  • I added more examples. I would like to check if all parameters are sent. – hoggar Nov 25 '19 at 02:04
  • how do you call the `Get` function? – shmnff Nov 25 '19 at 02:44
  • I added above to code. – hoggar Nov 25 '19 at 02:56

2 Answers2

0

In your 4 parameters example both string parameters are mandatory. That's why you get an error.
If you want them to be optional, assign them default value null. Than if statement would work.
Or add another Get() method.

David Pivovar
  • 115
  • 1
  • 10
  • All parameters are mandatory and I would like to execute IF if one of them is missing. – hoggar Nov 25 '19 at 03:04
  • By optional I meant to send request without parameters and verify them in controller. Otherwise you have to check parameters before sending request or handle error message. – David Pivovar Nov 25 '19 at 03:14
0

I believe that this another question have the answer of yours and corroborates what David Pivotar said.

awquadros
  • 519
  • 3
  • 4