0

I have a method as shown below.

public IResults GetResults(
            string code = "",
            string id = "",
            DateTime? registerDate = null,
            IEnumerable<Category> type = null,
            int pageNum = 1,
            bool all = false)
            {

                ....
                ........
            }

For every non-null or non-empty parameter I would like to have the parameter name and value to appended to the URI string.

For example, if the parameter code is a non-empty string then I would want it to be appended to the standard URI result/? as results/?code=passedcodevalue. If it is empty then there would be no need to append it.

Similarly, these are the rules to construct the URI:

  1. when code or id is not empty append it to - results/? - as

    results/?code=abc&id=xyz

  2. when registerDate is not null append it to - results/? - as results/?code=abc&id=xyz&registerdate=2004-06-29

  3. when type is not null append it to - results/? - as results/?code=abc&id=xyz&registerdate=2004-06-29&type=qwerty

  4. when pagenum is not 1 append it to - results/? - as results/?code=abc&id=xyz&registerdate=2004-06-29&type=qwerty&pagenum=5

  5. when all is not false append it to - results/? - as results/?code=abc&id=xyz&registerdate=2004-06-29&type=qwerty&pagenum=5&all=true

What is the optimal and generic way to do this?

GilliVilla
  • 4,998
  • 11
  • 55
  • 96
  • There are a couple of good answers at http://stackoverflow.com/questions/829080/how-to-build-a-query-string-for-a-url-in-c – Ryan M Jul 30 '14 at 18:17
  • @RyanM thanks! But I am also looking at non-null/non-empty to be genericized as well as parametr to name-value collection addition. – GilliVilla Jul 30 '14 at 18:20
  • If you want code that does this automatically, then that code will need to know the default values. You could get that through reflection but it would be a lot of work: http://msdn.microsoft.com/en-us/library/x0acewhc%28v=vs.110%29.aspx – Moby Disk Jul 30 '14 at 20:18

1 Answers1

0

Try this:

    public string GetResults(
        string code = "",
        string id = "",
        DateTime? registerDate = null,
        IEnumerable<Category> type = null,
        int pageNum = 1,
        bool all = false)
    {
        var result = "results/?";
        var firstFound = false;    //Used for add & to string
        for (int i = 0; i < 6; i++)
        {
            var partial = string.Empty;
            switch (i)
            {
                case 0:
                    partial = GetIfNotEmpty("code", code);
                    break;
                case 1:
                    partial = GetIfNotEmpty("id", id);
                    break;
                case 2:
                    partial = GetIfNotEmpty("registerDate", registerDate);
                    break;
                case 3:
                    partial = GetIfNotEmpty("type", type);
                    break;
                case 4:
                    partial = GetIfNotEmpty("pageNum", pageNum);
                    break;
                default:
                    partial = GetIfNotEmpty("all", all);
                    break;
            }
            if (string.IsNullOrWhiteSpace(partial))
                continue;
            if (firstFound)
                result += "&";
            else
                firstFound = true;
            result += partial;
        }
        return result;
    }

    private string GetIfNotEmpty<T>(string name, T value)
    {
        if (value == null)
            return string.Empty;
        if (value is IEnumerable)
        {
            var enumerableFinalString = string.Empty;
            foreach (var item in ((IEnumerable)value))
            {
                enumerableFinalString += item.ToString();
            }
            return string.Format("{0}={1}", name, enumerableFinalString);
        }
        return string.Format("{0}={1}", name, value.ToString());
    }
Max
  • 246
  • 5
  • 12