0

Lets say you have an object

public class SomeViewModel
    {
        public SomeViewModel()
        {
            this.SomeData = new List<SomeData>();
        }

        public string Name { get; set; }
        public string Surname{ get; set; }
        public List<SomeData> SomeData { get; set; }
    }

    public class SomeData
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

And now i want to pass the model as querystring from ASP.NET application to ASP.NET MVC application

string json = JsonConvert.SerializeObject(someModelVM);
//how to convert it to querystring ?
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + querystring, true);

so after redirect it would bind properly

public ActionResult SomeAction(SomeViewModel someViewModel)
{
//do something here
}

UPDATE

I've choosen the simples solution instead to complicating.

 string json = JsonConvert.SerializeObject(someModelVM);
 Response.Redirect("http://somedomain.com/SomeAction?redirect=" + json, true);


public ActionResult SomeAction(string json)
    {
        //try to deserialize json
        //security check the json
        //do stuff
    }
Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
  • 2
    Keep in mind that browsers have a limit of how much data can be sent over HTTP using the "GET" method. – Matthew Feb 13 '13 at 01:48

3 Answers3

1

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()))));
    }
}
Community
  • 1
  • 1
andres descalzo
  • 14,887
  • 13
  • 64
  • 115
0

Try this:

string json = JsonConvert.SerializeObject(someModelVM);
Response.Redirect("http://somedomain.com/SomeAction?redirect=" + HttpUtility.UrlEncode(json), true);

This uses the UrlEncode method to encode your JSON to be part of the URL as querystring

Hari Pachuveetil
  • 10,294
  • 3
  • 45
  • 68
0

I think you need to use HttpUtility.ParseQueryString for this purpose.

Check the documentation from msdn and this thread from Stackoverflow

Hope it helps.

Community
  • 1
  • 1
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60