1

I want to pass a Filters object as well as other things as query parameters into a url, for example something like:

   { 
       "clientId": 2, 
       "date": "2017-01-01",
       "filters": {
            "days": { "monday", "tuesday", "wednesday" },
            "months": { "january", "february" }
   }

But I don't know how an object like filters in this example could get passed in by a query string parameter. I know you would normally have something that looks like:

https://localhost/path?clientId=2&date=2017-01-01&filters= ?????

Thanks!

keenns
  • 863
  • 4
  • 14
  • 26
  • If you can't POST the data then you could encode that JSON string to base64 and pass it as one parameter then decode and deserialize on the server. – Crowcoder Jan 13 '17 at 18:48
  • It's hard to tell without knowing how the server expects the object. Is the server controlled by you? – cbr Jan 13 '17 at 18:48
  • the server is controlled by me yes and a POST then is probably possible...but i am unsure of how to do that also – keenns Jan 13 '17 at 23:43

3 Answers3

2

Maybe is better to POST your data if they are complex instead sending as query string parameter. But anyway, if you want to send as query string you can do following:

  1. Convert object into string
  2. Encode string
  3. Append as parameter

For following object (converted into string and removed spaces):

{
   "clientId": 2,
   "date": "2017-01-01",
   "filters": {
   "days": { "monday", "tuesday", "wednesday" },
   "months": { "january", "february" }
}

I created encoded text which is safe to sending over network:

%7B%0A%22clientId%22%3A%202%2C%0A%22date%22%3A%20%222017-01-01%22%2C%0A%22filters%22%3A%20%7B%0A%22days%22%3A%20%7B%20%22monday%22%2C%20%22tuesday%22%2C%20%22wednesday%22%20%7D%2C%0A%22months%22%3A%20%7B%20%22january%22%2C%20%22february%22%20%7D%0A%7D

In your case it will be:

https://localhost/path?clientId=2&date=2017-01-01&filters=%7B%0A%22clientId%22%3A%202%2C%0A%22date%22%3A%20%222017-01-01%22%2C%0A%22filters%22%3A%20%7B%0A%22days%22%3A%20%7B%20%22monday%22%2C%20%22tuesday%22%2C%20%22wednesday%22%20%7D%2C%0A%22months%22%3A%20%7B%20%22january%22%2C%20%22february%22%20%7D%0A%7D

You can use meyerweb.com to test encoding/decoding but in C# you can research HttpUtility.UrlEncode() method which can be used in your situation too.

kat1330
  • 5,134
  • 7
  • 38
  • 61
  • For curiousity's sake, how would one do a POST with this data then if that's better? – keenns Jan 13 '17 at 23:37
  • @keens12 I am assuming that you want to post from UI to your action on server side. You can make POST request with AJAX. Some good example i s explained here: http://stackoverflow.com/questions/4120212/mvc-ajax-json-post-to-controller-action-method When you send data with post they are placed into 'body' . – kat1330 Jan 13 '17 at 23:54
  • Isn't a post used to make a change in the database? And a get is used just to retrieve something? How is a post better in this case? – Chucky Nov 01 '18 at 13:15
  • 1
    @Chucky That is true. But what if you want to send payload? How you can do this with `GET`? – kat1330 Nov 01 '18 at 16:46
0

If you really want to pass parameters in a query string, this is an example using ASP.NET MVC.

Create a route: routes.MapRoute( name: "Custom", url: "{controller}/{action}/{clientId}/{date}/{filtersDay}/{filtersMonth}", defaults: new { controller = "Home", action = "CustomQueryString" } );

You can repeat an item and the Model Binding will create a string array: http://localhost/Home/CustomQueryString?clientId=1&date=2017-01-01&filtersDay=Friday&filtersDay=Monday&filtersMonth=April&filtersMonth=June

and then, you will have this: enter image description here

0

I have a ToDictionary extension method, that converts objects into the query string and you can pass it via RouteValues, I pass Model.SearchCriteria, which is a complex object in the folllowing example:

<a href='@Url.Action("ExportAll", 
    new RouteValueDictionary(Model.SearchCriteria))'>Export All</a>

ToDictionary is an extension method:

public static class ToDictionaryExtensionMethod
{
    public static IDictionary<string, object> ToDictionary(this object source)
    {
        return source.ToDictionary<object>();
    }
}

Unfortunately the following code doesn't work:

@Html.ActionLink("Export All", "ExportAll", 
      new RouteValueDictionary(Model.SearchCriteria.ToDictionary()), 
      new { @class = "btn btn-default" })

This is because this version of ActionLink accepts routevalues as an object, not as a RouteValueDictionary (in MVC 5). So to make it work, I have to convert the Html attributes to a dictionary as well which uses the correct overload of Html.ActionLink:

@Html.ActionLink("Export All", "ExportAll", 
      new RouteValueDictionary(Model.SearchCriteria.ToDictionary()), 
      new Dictionary<string,object>{{"class","btn btn-default"}})
Philip Johnson
  • 1,091
  • 10
  • 24