【问题标题】:how to modify HttpRequest QueryString value in class library?如何修改类库中的 HttpRequest QueryString 值?
【发布时间】:2011-12-05 02:41:41
【问题描述】:

如何修改类库中的HttpRequest QueryString 值,我目前可以获取QueryString 值并进行修改,但是如何将修改后的QueryString 写入HttpRequest:

public static HttpRequest ModiQueryString(HttpRequest request)
{
    var nv = new NameValueCollection(request.QueryString);
    foreach (string key in nv.Keys)
    {
        nv[key] = "abc";
    }
    //here how to let request.QueryString equal nv
    return request;
}

因为 request.QueryString 是只读的,我该怎么办? 谁能帮帮我?谢谢

如果我创建一个新的 HttpRequest,我可以使用:

StringBuilder newQuery=new StringBuilder();
foreach(string key in nv.Keys)
{
    newQuery.AppendFormat("{0}={1}",key,FilterKeyWord(nv[key]));
}
HttpRequest newRequest = new HttpRequest("", request.Url, newQuery.ToString());
return newRequest;

【问题讨论】:

  • 为什么要,为什么要修改查询字符串?
  • 我想创建一个通用服务来过滤关键字

标签: c# query-string httprequest


【解决方案1】:

这根本不可能,因为QueryString 是不可设置的,并且类是sealed 引导。

您应该在构造请求之前修改查询字符串,或者如果您只有一个 HttpRequest 可以使用,则保留查询字符串的临时副本(您可以编辑)并构造另一个 HttpRequest修改后的查询字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多