How can I add values to querystring?
I’m trying to do this:
String currurl = HttpContext.Current.Request.RawUrl;
var querystring = HttpContext.Current.Request.QueryString.ToString();
var PrintURL = currurl + (String.IsNullOrEmpty(querystring)) ?
HttpContext.Current.Request.QueryString.Add("print", "y") : string.Empty;
But I keep getting this error:
Cannot implicitly convert type ‘string’ to ‘bool’
all i’m trying to do is get current url and add ?pring=y to querystring
Well, the first problem can be solved using this instead:
All that’s changed from your original code is simply moving the closing paren from
(String.IsNullOrEmpty(querystring))(where it was unnecessary) to the end of the?:clause. This makes it explicitly clear what you’re trying to do.Otherwise, the compiler tries to concatenate the result of
String.IsNullOrEmpty(querystring)(which is abool) tocurrUrl— incorrect, and not what you intended in the first place.However, you’ve got a second problem with the
HttpContext.Current.Request.QueryString.Add("print", "y")statement. This returnsvoid, not astring. You’ll need to modify this part of your ternary expression so that it returns a string — what are you trying to do?