Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querystring - Add values to querystring in c#

Tags:

c#

.net

asp.net

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

like image 981
ShaneKm Avatar asked Feb 24 '11 18:02

ShaneKm


1 Answers

Well, the first problem can be solved using this instead:

var PrintURL = currurl + (String.IsNullOrEmpty(querystring) ? 
   HttpContext.Current.Request.QueryString.Add("print", "y") : string.Empty);

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 a bool) to currUrl -- 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 returns void, not a string. You'll need to modify this part of your ternary expression so that it returns a string -- what are you trying to do?

like image 180
Donut Avatar answered Sep 28 '22 20:09

Donut