开发者

changing the Request.QueryString value

how can i modified the querystring?

I have capture the query string like this

qs = Request.QueryString["flag"].ToString();

and then rebuilt the query string with modified values and response.redirect(url &a开发者_如何学运维mp; qs) to it


While I'm not sure I'd suggest using this approach liberally, if you wanted to reconstruct the path and query string with a few changes... you could convert the query string to an editable collection, modify it, then rebuild it from your new collection.

Goofy example...

// create dictionary (editable collection) of querystring
var qs = Request.QueryString.AllKeys
            .ToDictionary(k => k, k => Request.QueryString[k]);

// modify querystring
qs["flag"] = "2";

// rebuild querystring
var redir = string.Format("{0}{1}", Request.Path,
            qs.Aggregate(new StringBuilder(),
                 (sb, arg) => sb.AppendFormat("{0}{1}={2}", 
                 sb.Length > 0 ? "&" : "?", arg.Key, arg.Value)));

// do something with it
Response.Redirect(redir);

While I definitely wouldn't recommend the below for production code, for testing purposes you can use reflection to make the querystring collection editable.

// Get the protected "IsReadOnly" property of the collection
System.Reflection.PropertyInfo prop = Request.QueryString.GetType()
    .GetProperty("IsReadOnly", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

// Set the property false (writable)
prop.SetValue(Request.QueryString, false, null);

// Have your way with it.
Request.QueryString.Add("flag", "2");


To combine the required destination URL based on the Request’s properties, use something like this:

string destUrl = string.Format("{0}://{1}{2}/", Request.Url.Scheme, Request.Url.Authority, Request.Url.AbsolutePath);
if (destUrl.EndsWith("/"))
    destUrl = destUrl.TrimEnd(new char[] { '/' });
if (!string.IsNullOrEmpty(Request.QueryString["paramName"])) {
    destUrl = string.Format("{0}?paramName={1}", destUrl, "paramValueHere");
    Response.Redirect(destUrl);
}


i am not sure if I understand your question. You can just alter the string qs and use.

qs = qs + "modification"    
Response.Redirect("this.aspx?flag=" + qs )


The stuff in the Request class deals with the request that got you to the page. You can't edit it because the client constructed it, not the server.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜