Skip to content

Instantly share code, notes, and snippets.

@DanDiplo
Last active February 7, 2019 16:38
Show Gist options
  • Select an option

  • Save DanDiplo/a89c3ec146e1bbd1a26e to your computer and use it in GitHub Desktop.

Select an option

Save DanDiplo/a89c3ec146e1bbd1a26e to your computer and use it in GitHub Desktop.

Revisions

  1. DanDiplo renamed this gist May 31, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. DanDiplo revised this gist May 31, 2015. 1 changed file with 19 additions and 21 deletions.
    40 changes: 19 additions & 21 deletions QueryStringHelper
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,33 @@
    public static class QueryStringHelperExamples
    {
    public static void Examples()
    {
    QueryStringHelper qs1 = new QueryStringHelper(HttpContext.Current.Request.QueryString); // initialise from Request.QueryString
    // Code examples of using my QueryStringHelper c# class for parsing query string values

    string query = "?page=5&username=dan&year=2010&enabled=true&email=dan@example.com&option=apple&option=banana&option=melon&date=2015/07/06";
    // See https://github.com/DanDiplo/QueryString-Helper

    QueryStringHelper qs = new QueryStringHelper(query); // intialise from string
    QueryStringHelper qs1 = new QueryStringHelper(Request.QueryString); // initialise from Request.QueryString

    string username = qs.GetValueByName("username"); // username = "dan"
    string query = "?page=5&username=dan&year=2010&enabled=true&[email protected]&option=apple&option=banana&option=melon&date=2015/07/06";

    qs.Add("category", "products"); // adds a new key called "category" with the value "products"
    QueryStringHelper qs = new QueryStringHelper(query); // intialise from string

    qs.AddOrReplace("year", 1999); // changes the year value from "2010" to "1999"
    string username = qs.GetValueByName("username"); // username = "dan"

    int year = qs.GetValueByName<int>("year"); // year = 1999
    qs.Add("category", "products"); // adds a new key called "category" with the value "products"

    qs.AddOrReplace("page", 6); // changes the value of "page" to "6"
    qs.AddOrReplace("year", 1999); // changes the year value from "2010" to "1999"

    bool enabled = qs.GetValueByName<bool>("enabled"); // enabled = true
    int year = qs.GetValueByName<int>("year"); // year = 1999

    qs.RemoveByName("email"); // removes the "email" key
    qs.AddOrReplace("page", 6); // changes the value of "page" to "6"

    IEnumerable<string> options = qs.GetMultipleValuesByName("option"); // options[0] = "apple", options[1] = "banana", options[2] = "melon"
    bool enabled = qs.GetValueByName<bool>("enabled"); // enabled = true

    DateTime date = qs.GetValueByName<DateTime>("date");
    qs.RemoveByName("email"); // removes the "email" key

    var mailAddress = qs.GetValueByName<System.Net.Mail.MailAddress>("email", e => new System.Net.Mail.MailAddress(e)); // use a delegate func
    IEnumerable<string> options = qs.GetMultipleValuesByName("option"); // options[0] = "apple", options[1] = "banana", options[2] = "melon"

    string querystring = qs.ToString(); // qs = "page=6&username=dan&year=1999&enabled=true&option=apple%2cbanana%2cmelon&date=2015%2f07%2f06&category=products";
    DateTime date = qs.GetValueByName<DateTime>("date");

    int count = qs.Count(); // count = 7
    }
    }
    var mailAddress = qs.GetValueByName<System.Net.Mail.MailAddress>("email", e => new System.Net.Mail.MailAddress(e)); // use a delegate func

    string querystring = qs.ToString(); // qs = "page=6&username=dan&year=1999&enabled=true&option=apple%2cbanana%2cmelon&date=2015%2f07%2f06&category=products";

    int count = qs.Count(); // count = 7
  3. DanDiplo revised this gist May 31, 2015. 1 changed file with 21 additions and 19 deletions.
    40 changes: 21 additions & 19 deletions QueryStringHelper
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,35 @@
    // Code examples of using my QueryStringHelper c# class for parsing query string values
    public static class QueryStringHelperExamples
    {
    public static void Examples()
    {
    QueryStringHelper qs1 = new QueryStringHelper(HttpContext.Current.Request.QueryString); // initialise from Request.QueryString

    // See https://github.com/DanDiplo/QueryString-Helper
    string query = "?page=5&username=dan&year=2010&enabled=true&email=dan@example.com&option=apple&option=banana&option=melon&date=2015/07/06";

    QueryStringHelper qs1 = new QueryStringHelper(Request.QueryString); // initialise from Request.QueryString
    QueryStringHelper qs = new QueryStringHelper(query); // intialise from string

    string query = "?page=5&username=dan&year=2010&enabled=true&[email protected]&option=apple&option=banana&option=melon&date=2015/07/06";
    string username = qs.GetValueByName("username"); // username = "dan"

    QueryStringHelper qs = new QueryStringHelper(query); // intialise from string
    qs.Add("category", "products"); // adds a new key called "category" with the value "products"

    string username = qs.GetValueByName("username"); // username = "dan"
    qs.AddOrReplace("year", 1999); // changes the year value from "2010" to "1999"

    qs.Add("category", "products"); // adds a new key called "category" with the value "products"
    int year = qs.GetValueByName<int>("year"); // year = 1999

    qs.AddOrReplace("year", 1999); // changes the year value from "2010" to "1999"
    qs.AddOrReplace("page", 6); // changes the value of "page" to "6"

    int year = qs.GetValueByName<int>("year"); // year = 1999
    bool enabled = qs.GetValueByName<bool>("enabled"); // enabled = true

    qs.AddOrReplace("page", 6); // changes the value of "page" to "6"
    qs.RemoveByName("email"); // removes the "email" key

    bool enabled = qs.GetValueByName<bool>("enabled"); // enabled = true
    IEnumerable<string> options = qs.GetMultipleValuesByName("option"); // options[0] = "apple", options[1] = "banana", options[2] = "melon"

    qs.RemoveByName("email"); // removes the "email" key
    DateTime date = qs.GetValueByName<DateTime>("date");

    IEnumerable<string> options = qs.GetMultipleValuesByName("option"); // options[0] = "apple", options[1] = "banana", options[2] = "melon"
    var mailAddress = qs.GetValueByName<System.Net.Mail.MailAddress>("email", e => new System.Net.Mail.MailAddress(e)); // use a delegate func

    DateTime date = qs.GetValueByName<DateTime>("date");
    string querystring = qs.ToString(); // qs = "page=6&username=dan&year=1999&enabled=true&option=apple%2cbanana%2cmelon&date=2015%2f07%2f06&category=products";

    var mailAddress = qs.GetValueByName<System.Net.Mail.MailAddress>("email", e => new System.Net.Mail.MailAddress(e)); // use a delegate func

    string querystring = qs.ToString(); // qs = "page=6&username=dan&year=1999&enabled=true&option=apple%2cbanana%2cmelon&date=2015%2f07%2f06&category=products";

    int count = qs.Count(); // count = 7
    int count = qs.Count(); // count = 7
    }
    }
  4. DanDiplo created this gist May 31, 2015.
    33 changes: 33 additions & 0 deletions QueryStringHelper
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // Code examples of using my QueryStringHelper c# class for parsing query string values

    // See https://github.com/DanDiplo/QueryString-Helper

    QueryStringHelper qs1 = new QueryStringHelper(Request.QueryString); // initialise from Request.QueryString

    string query = "?page=5&username=dan&year=2010&enabled=true&[email protected]&option=apple&option=banana&option=melon&date=2015/07/06";

    QueryStringHelper qs = new QueryStringHelper(query); // intialise from string

    string username = qs.GetValueByName("username"); // username = "dan"

    qs.Add("category", "products"); // adds a new key called "category" with the value "products"

    qs.AddOrReplace("year", 1999); // changes the year value from "2010" to "1999"

    int year = qs.GetValueByName<int>("year"); // year = 1999

    qs.AddOrReplace("page", 6); // changes the value of "page" to "6"

    bool enabled = qs.GetValueByName<bool>("enabled"); // enabled = true

    qs.RemoveByName("email"); // removes the "email" key

    IEnumerable<string> options = qs.GetMultipleValuesByName("option"); // options[0] = "apple", options[1] = "banana", options[2] = "melon"

    DateTime date = qs.GetValueByName<DateTime>("date");

    var mailAddress = qs.GetValueByName<System.Net.Mail.MailAddress>("email", e => new System.Net.Mail.MailAddress(e)); // use a delegate func

    string querystring = qs.ToString(); // qs = "page=6&username=dan&year=1999&enabled=true&option=apple%2cbanana%2cmelon&date=2015%2f07%2f06&category=products";

    int count = qs.Count(); // count = 7