6

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ShaneKm
  • 20,823
  • 43
  • 167
  • 296

6 Answers6

10

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?

Donut
  • 110,061
  • 20
  • 134
  • 146
  • 1
    Correct, but you need to explain why. – SLaks Feb 24 '11 at 18:28
  • @Donut Isn't HttpContext.Current.Request.QueryString readonly ? – Magnus Feb 24 '11 at 18:33
  • @Donut: As mentioned, this will fail because `Request.QueryString.Add()` does not return anything (void). – Nimrod Feb 24 '11 at 18:44
  • @Magnus Yes it is. Not sure what the OP is expecting to be returned by that expression; seems like he just wants to concatenate another string, but some clarification would be nice. – Donut Feb 24 '11 at 18:44
  • i tried this: var currurl = HttpContext.Current.Request.RawUrl; var querystring = HttpContext.Current.Request.QueryString; var PrintURL = currurl + (String.IsNullOrEmpty(querystring) ? HttpContext.Current.Request.QueryString.Add("print", "y") : string.Empty); but i still get error :The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments – ShaneKm Feb 24 '11 at 18:47
  • @Shane Km That's because in your comment, `querystring` is **not a string** -- it's a `NameValueCollection`. If you're trying to determine whether this collection is empty or not, you could check its [`Count` property](http://msdn.microsoft.com/en-us/library/system.collections.specialized.nameobjectcollectionbase.count.aspx). – Donut Feb 24 '11 at 18:51
5

HttpContext.Current.Request.QueryString.Add("print", "y") returns void, not a string, so you can't use that call in the ternary expression. Plus, adding to the querystring on the Request won't affect your HTTPResponse, and I'm assuming that's what you want to do. You need to craft the new URL and use response.redirect to have the browser load the new url with the updated querystring.

Tony
  • 664
  • 3
  • 3
  • 10
    HttpContext.Current.Request.QueryString is readonly so HttpContext.Current.Request.QueryString.Add("print", "y") will raise an exception. – Anthony May 01 '11 at 11:58
3

i figured it out.

String currurl = HttpContext.Current.Request.Url.ToString();
String querystring = null;

// Check to make sure some query string variables
// exist and if not add some and redirect.
int iqs = currurl.IndexOf('?');
if (iqs == -1)
{
    String redirecturl = currurl + "?print=y";
}

not sure if this is the cleanest way but it works. thanks all for help

ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • +1. And for the sake of elegance: String url = HttpContext.Current.Request.Url.ToString(); url += url.IndexOf('?') == -1 ? "?print=y" : "print=y"; –  Jun 20 '12 at 15:53
2

There's a couple things wrong here with what you're trying to do.

The first thing is that the QueryString collection is a NameValueCollection. The Add method has a void return. So even trying to assign the result of QueryString.Add isn't going to work.

Second, you can't modify the QueryString collection. It's read-only. There's a response over on Velocity Reviews that talks to exactly what you're trying to do. Instead of trying to modify the query string, you should redirect the user with the new value.

villecoder
  • 13,323
  • 2
  • 33
  • 52
1
currurl + (String.IsNullOrEmpty(querystring)

has to return a boolean so condition has to be different.

Praneeth
  • 2,527
  • 5
  • 30
  • 47
1

First problem is you need brackets around your statement that is using the ?:

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

The next problem is that HttpContext.Current.Request.QueryString.Add does not return anything so one side of the : returns void where the other returns and empty string.

Kelsey
  • 47,246
  • 16
  • 124
  • 162