English 中文(简体)
删除具体内容 学历
原标题:Removing specific QueryStrings
  • 时间:2010-03-23 00:03:56
  •  标签:
  • c#

换成一页,可以有多个查询,例如<编码>books.aspx?author=Arthor&level=4&year=2004。

我愿建立一个明确具体问题的州。

例如,在点击<代码>clearAuthorBtn时,用户应当改用books.aspx?level=4&year=<2004

How can I make it? Thank you very much.

最佳回答

这里是一种可以帮助的方法。 我没有测试这一具体执行情况,但同样如此(而且相当有力)。

public static string GetQueryStringWithoutKey(HttpRequest request, string keyToRemove) {

    // Assert keyToRemove is not null.
    if (keyToRemove == null) {
        throw new ArgumentNullException("keyToRemove");
    }

    // If the QueryString has no data, simply return an empty string.
    if (request.QueryString.AllKeys.Length == 0) {
        return string.Empty;
    }

    // Reconstruct the QueryString with everything except the existing key/value pair.
    StringBuilder queryStringWithoutKey = new StringBuilder();
    for (int i = 0; i < request.QueryString.AllKeys.Length; i++) {

        // Only append data that is not the given key/value pair.
        if (request.QueryString.AllKeys[i] != null &&
            request.QueryString.AllKeys[i].ToLower() != keyToRemove.ToLower()) {
            queryStringWithoutKey.Append(request.QueryString.AllKeys[i]);
            queryStringWithoutKey.Append("=");
            queryStringWithoutKey.Append(request.QueryString[i]);
            queryStringWithoutKey.Append("&");
        }
    }

    // We might have had a key, but if the only key was Message, then there is no
    // data to return for the QueryString.
    if (queryStringWithoutKey.Length == 0) {
        return string.Empty;
    }

    // Remove trailing ampersand.
    return queryStringWithoutKey.ToString().TrimEnd( & );
}

你可以称之为上述方法(我使用HttpContext)。 现时,请在<代码>Page或<编码>外打电话:

HttpRequest request = HttpContext.Current.Request;
string url = request.ServerVariables["PATH_INFO"];
string queryString = GetQueryStringWithoutKey(request, "author");
if (!string.IsNullOrEmpty(queryString) {
    url += "?" + queryString;
}
HttpContext.Current.Response.Redirect(url);
问题回答

ASP. NET,C#, 如同这种假装编码一样,应当在你 but子的活动手里工作:

  foreach (var key in Request.QueryString)
  {
    string url = "books.aspx?";
    if (key != "author")
    {
      url = url + Server.UrlEncode(key) + "=" + Server.UrlEncode(Request.QueryString[key]) + "&";
    }
    Response.Redirect(url);
  }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签