Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Request object keys

Tags:

c#

asp.net

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!

like image 961
s15199d Avatar asked Jul 28 '11 17:07

s15199d


People also ask

How to loop through object object?

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.

How to iterate over object keys?

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.

How do I iterate an object in node JS?

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.


2 Answers

use Request.Params:

foreach (string s in Request.Params.Keys )     
{       
    Response.Write(s.ToString() + ":" + Request.Params[s] + "<br>");       
}
like image 141
Mark Cidade Avatar answered Sep 22 '22 14:09

Mark Cidade


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]);
    }
like image 30
Jon Segura Avatar answered Sep 21 '22 14:09

Jon Segura