开发者

Querystring - Add values to querystring in c#

How can I add values to querystring?

I'm tr开发者_运维问答ying 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:

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?


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.


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


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.


currurl + (String.IsNullOrEmpty(querystring)

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜