Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array values in an HTTP request in .NET

What's the standard way of passing and processing an array in an HTTP request in .NET? I have a solution, but I don't know if it's the best approach.

Here's my solution:

<form action="myhandler.ashx" method="post">
    <input type="checkbox" name="user" value="Aaron" />
    <input type="checkbox" name="user" value="Bobby" />
    <input type="checkbox" name="user" value="Jimmy" />
    <input type="checkbox" name="user" value="Kelly" />
    <input type="checkbox" name="user" value="Simon" />
    <input type="checkbox" name="user" value="TJ" />

    <input type="submit" value="Submit" />
</form>

The ASHX handler receives the "user" parameter as a comma-delimited string. You can get the values easily by splitting the string:

public void ProcessRequest(HttpContext context)
    {
        string[] users = context.Request.Form["user"].Split(',');
    }

So, I already have an answer to my problem: assign multiple values to the same parameter name, assume the ASHX handler receives it as a comma-delimited string, and split the string. My question is whether or not this is how it's typically done in .NET.

What's the standard practice for this? Is there a simpler way to grab the multiple values than assuming that the value is comma-delimited and calling Split() on it? Is this how arrays are typically passed in .NET, or is XML used instead?

Does anyone have any insight on whether or not this is the best approach?

like image 377
JR. Avatar asked May 05 '10 20:05

JR.


People also ask

How do you pass an array to a method in C#?

You can pass an initialized single-dimensional array to a method. For example, the following statement sends an array to a print method. int[] theArray = { 1, 3, 5, 7, 9 }; PrintArray(theArray); The following code shows a partial implementation of the print method.

Does C# pass arrays by reference or value?

Yes, they are passed by reference by default in C#. All objects in C# are, except for value types. To be a little bit more precise, they're passed "by reference by value"; that is, the value of the variable that you see in your methods is a reference to the original object passed.

How do you pass an array as a parameter?

To pass an array as a parameter to a function, pass it as a pointer (since it is a pointer). For example, the following procedure sets the first n cells of array A to 0. Now to use that procedure: int B[100]; zero(B, 100);


2 Answers

If you are using checkboxes or a multi-select list box, then a comma-separated set of values is what you get automatically from html. What you are doing is perfectly fine. If you generate an array in your javascript in some other way, you could generate comma-separated string and assign it to a hidden field, and use split() on the server in the same way. XML is certainly another option, but it seems to complex to me if all you want to do is pass a simple array of numbers or short strings. (Of course, if the string values you need to pass contain commas, this would screw up your simple plan.)

like image 41
Ray Avatar answered Oct 13 '22 10:10

Ray


There isn't really a standard, but what you are using is the closest to it.

However, the values are actually not sent as a comma separated string, they are sent as separate values with the same name. The form data from your example will look like this:

user=Aaron&user=Bobby&user=Jimmy&user=Kelly&user=Simon&user=TJ

You can read the values as an array directly like this:

string[] users = context.Request.Form.GetValues("user");

If you use Form["user"] it will concatenate the values for you and you have to split them again. This is just a waste of time, and it also breaks if any of the values contains a comma.

like image 101
Guffa Avatar answered Oct 13 '22 09:10

Guffa