6

How can I add values to a 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 the querystring.

7 Answers 7

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 bracket 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?

Sign up to request clarification or add additional context in comments.

6 Comments

Correct, but you need to explain why.
@Donut Isn't HttpContext.Current.Request.QueryString readonly ?
@Donut: As mentioned, this will fail because Request.QueryString.Add() does not return anything (void).
@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.
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
|
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.

1 Comment

HttpContext.Current.Request.QueryString is readonly so HttpContext.Current.Request.QueryString.Add("print", "y") will raise an exception.
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.

1 Comment

+1. And for the sake of elegance: String url = HttpContext.Current.Request.Url.ToString(); url += url.IndexOf('?') == -1 ? "?print=y" : "print=y";
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.

Comments

1
currurl + (String.IsNullOrEmpty(querystring)

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

Comments

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.

Comments

0

According to the Microsoft Documentation at: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.querystring.add?view=aspnetcore-9.0

Request.QueryString.Add returns a QueryString, not void. However, Request.QueryString.Add does not mutate the Request Query String, therefore, you need to do something like this:

Request.QueryString = Request.QueryString.Add("returnUrl", url.ToString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.