This works to loop through all Form keys:
foreach (string s in Request.Form.Keys )
{
Response.Write(s.ToString() + ":" + Request.Form[s] + "<br>");
}
But, I want to loop through all Request keys:
foreach (string s in Request )
{
Response.Write(s.ToString() + ":" + Request[s] + "<br>");
}
Problem is request.keys is not a collection. But clearly the request object has children that I want to loop through. I'm pretty sure it's possible, I'm just using bad syntax.
thanks in advance!
The better way to loop through objects is first to convert the object into an array. Then, you loop through the array. You can convert an object into an array with three methods: Object.
The Object. keys() method was introduced in ES6. It takes the object that you want to iterate over as an argument and returns an array containing all properties names (or keys). You can then use any of the array looping methods, such as forEach(), to iterate through the array and retrieve the value of each property.
Since Javascript 1.7 there is an Iterator object, which allows this: var a={a:1,b:2,c:3}; var it=Iterator(a); function iterate(){ try { console. log(it. next()); setTimeout(iterate,1000); }catch (err if err instanceof StopIteration) { console.
use Request.Params
:
foreach (string s in Request.Params.Keys )
{
Response.Write(s.ToString() + ":" + Request.Params[s] + "<br>");
}
Mark is correct this will work but it will return all the keys in cookies, the keys in form that is being sent , and the keys in the query strings and other key value pairs being sent. I suggest getting more specific. If you are receiving a Post object use
Dictionary<string, string> _properties;
foreach (string f in report.Form.Keys)
{
_properties.Add(f, report.Form[f]);
}
and for a Get page use
foreach(string s in report.QueryString.Keys)
{
_properties.Add(s,report.QueryString[s]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With