Reputation: 21328
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
Upvotes: 6
Views: 44198
Reputation: 21328
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
Upvotes: 3
Reputation: 112825
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?
Upvotes: 10
Reputation: 13483
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.
Upvotes: 2
Reputation: 47726
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.
Upvotes: 1
Reputation: 2547
currurl + (String.IsNullOrEmpty(querystring)
has to return a boolean so condition has to be different.
Upvotes: 1
Reputation: 684
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.
Upvotes: 5