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:
when
codeoridis not empty append it to -results/?- asresults/?code=abc&id=xyz
when
registerDateis not null append it to -results/?- as results/?code=abc&id=xyz®isterdate=2004-06-29when
typeis not null append it to -results/?- as results/?code=abc&id=xyz®isterdate=2004-06-29&type=qwertywhen
pagenumis not 1 append it to -results/?- as results/?code=abc&id=xyz®isterdate=2004-06-29&type=qwerty&pagenum=5when
allis not false append it to -results/?- as results/?code=abc&id=xyz®isterdate=2004-06-29&type=qwerty&pagenum=5&all=true
What is the optimal and generic way to do this?