You can see this example to convert Class to "query string"
for example with the class you have, would have to be something like this:
Name = My name
Surname = My Surname
SomeData = [
{
Name = My SD0Name,
Value = My SD0Value
},
{
Name = My SD1Name,
Value = My SD1Value
}
]
Name=My%20name&Surname=My%20Surname&SomeData[0].Name=My%20SD0Name&SomeData[0].Value=My%20SD0Value&SomeData[1].Name=My%20SD1Name&SomeData[1].Value=My%21SD0Value
then you have to concatenate your url with the new text:
var someViewModel = new ToQueryString { Name = "My name", ... };
var querystring = someViewModel.ToQueryString();
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + querystring, true);
you do not need HttpUtility.UrlEncode
because the extension is already dealing do this.
EDIT for @Matthew comment. If you have a large query string, you can use a tool like this list to zip query string, then contatenate a value:
c-compress-and-decompress-strings
compression-decompression-string-with-c-sharp
In this case you can use a Json format, already that you send the zip of the text in a variable. But you need change then action that receives this parameter:
Response.Redirect("http://somedomain.com/SomeAction?redirectZip=" + jsonStringZip, true);
Code from this blog:
public static class UrlHelpers
{
public static string ToQueryString(this object request, string separator = ",")
{
if (request == null)
throw new ArgumentNullException("request");
// Get all properties on the object
var properties = request.GetType().GetProperties()
.Where(x => x.CanRead)
.Where(x => x.GetValue(request, null) != null)
.ToDictionary(x => x.Name, x => x.GetValue(request, null));
// Get names for all IEnumerable properties (excl. string)
var propertyNames = properties
.Where(x => !(x.Value is string) && x.Value is IEnumerable)
.Select(x => x.Key)
.ToList();
// Concat all IEnumerable properties into a comma separated string
foreach (var key in propertyNames)
{
var valueType = properties[key].GetType();
var valueElemType = valueType.IsGenericType
? valueType.GetGenericArguments()[0]
: valueType.GetElementType();
if (valueElemType.IsPrimitive || valueElemType == typeof (string))
{
var enumerable = properties[key] as IEnumerable;
properties[key] = string.Join(separator, enumerable.Cast<object>());
}
}
// Concat all key/value pairs into a string separated by ampersand
return string.Join("&", properties
.Select(x => string.Concat(
Uri.EscapeDataString(x.Key), "=",
Uri.EscapeDataString(x.Value.ToString()))));
}
}