0

How to get all string values from action arguments in Web API C# method I have used this code:

 public override void OnActionExecuting(HttpActionContext actionContext)
 {

     Dictionary<string, object> list = actionContext.ActionArguments;

     for (int index = 0; index < list.Count; index++)
     {
       // need to all variables if the data type is string only 

       // input parameter might be list or model or int or string

       list.ElementAt(index).Value;

     }
 }

I need to write generic method to validate all input parameters if the input parameter is string. Input parameters might be string or object or int or model or list in model...So, i need to validate any kind of input to get all string parameters

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • 1
    Check this out! https://stackoverflow.com/a/983061/1513722 – Maddie Sep 23 '20 at 05:35
  • so, `if (params[index] is string)` should do the trick – Stefan Sep 23 '20 at 05:39
  • I need to verify all input arguments. So, please help me with IF condtions.. Input arguments might be List or string or int or some model like that – Suresh Gandham Sep 23 '20 at 05:45
  • I need to write generic method to validate all input parameters if the input parameter is string. Input parameters might be string or object or int or model or list in model...So, i need to validate any kind of input to get all string parameters – Suresh Gandham Sep 23 '20 at 05:49

1 Answers1

0

On your request; this should work:

public override void OnActionExecuting(HttpActionContext actionContext)
{
    var dictionary = actionContext.ActionArguments; 
    //key will contain the key, for convenience.
    foreach (var key in dictionary.Keys)
    {
        //the value
        var val = dictionary[key];

        if (val is string)
        {
            //the value is runtime type of string
            //do what you want with it.
        }
    }
}

As for the follow up, have a look at this: https://stackoverflow.com/a/20554262/2416958 . Combine with the "PrintProperties" method, and you should get somewhere.

Be aware of cyclic references though ;-)

Modified version (credits: https://stackoverflow.com/a/20554262/2416958):

note: is not protected against cyclic references.

public void ScanProperties(object obj)
{    
    if (obj == null) return;
    Type objType = obj.GetType();
    PropertyInfo[] properties = objType.GetProperties();
    foreach (PropertyInfo property in properties)
    {
        object propValue = property.GetValue(obj, null);
        var elems = propValue as IEnumerable;
        if (elems != null)
        {
            foreach (var item in elems)
            {
                ScanProperties(item);
            }
        }
        else
        {
            // This will not cut-off System.Collections because of the first check
            if (propValue is string)
            {
                //do you validation routine
            }

            ScanProperties(propValue);
        }
    }
}

And call it like:

public override void OnActionExecuting(HttpActionContext actionContext)
{
    var dictionary = actionContext.ActionArguments; 
    //key will contain the key, for convenience.
    foreach (var key in dictionary.Keys)
    {
        //the value
        ScanProperties(dictionary[key]);
    }
}
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • This is fine if the value type is directly string and If the input parameter is some model or list of string... i need to get strings from them also.... my intension is to validate all string values from list or model or from any kind of input – Suresh Gandham Sep 23 '20 at 06:15
  • Ah, ok, please add that info to your question, as "edit". It helps people to answer your question when you are precise. You'll need to go recursively to all the properties. – Stefan Sep 23 '20 at 06:17
  • @SureshGandham: this should help https://stackoverflow.com/a/20554262/2416958 – Stefan Sep 23 '20 at 06:20