2

When I get the querystring with Request.Url.Query I'd like to exclude a parameter (without using replace, or function like these).

So, for example, if I have this querystring :

?ID=1241&IDL=241&HC=1241

I'd like to exclude IDL getting :

?ID=1241&HC=1241

Is this a implemented way or I need to make my own function?

EDIT : I care about the order of my query string values.

9
  • Do you mean replace the Query property in the Uri object to "fool downstream" or just getting a string not including it? Commented Jan 19, 2012 at 16:50
  • not including it...! Also because I don't know what do you mean with "fool downstream" :) Commented Jan 19, 2012 at 16:50
  • @markzzz I think he means "do you want other calls of Request.Url.Query later on to beleive that the query string value isn't there?" Commented Jan 19, 2012 at 16:54
  • 1
    The querystring cannot be modified. but you can parse it (it's a KeyValuePair collection) and modify the collection, and then restore it back (via a Response.Redirect("page.aspx?" + modifiedQS)) Commented Jan 19, 2012 at 16:55
  • 1
    on a side note, caring about the order of query string params doesn't sound right to me Commented Jan 20, 2012 at 13:02

2 Answers 2

4

It's still string manipulation, but how about something like:

String.Concat("?",
    String.Join("&",
        Request.Url.Query.Substring(1)
            .Split('&')
            .Where(k => !k.StartsWith("IDL="))
            .ToArray() // For .NET versions prior to v4.0
     )
)

Alternatively you could use Request.QueryString to get at a processed collection of query-string parameters.

EDIT: This will leave your parameters in the order they were sent.

Here's a sample ASPX page that outputs a modified query-string (I've tested it in a ASP.NET 3.5 Web-Site):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Linq" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Query String Removal</title>
</head>
<body>
    Modified query-string: <%=
        String.Concat("?",
            String.Join("&",
                Request.Url.Query.Substring(1)
                    .Split('&')
                    .Where(k => !k.StartsWith("IDL="))
                    .ToArray() // For .NET versions prior to v4.0
             )
        )
    %>
</body>
</html>

NOTE: System.Linq has been imported. An exception will be raised if you don't specify a query-string.

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

6 Comments

And from this can I remove directly a parameter?
I take it you mean from the Request.QueryString collection, I'm afraid not, it's a read-only collection :-[, as mentioned by Oxonhammer.
added (string[]) before Request.Url.Query...., but now it says System.InvalidCastException: Impossibile eseguire il cast di oggetti di tipo 'WhereArrayIterator1[System.String]' sul tipo 'System.String[]'.`
What version of .NET are you using? The code I posted I tried in a .NET ASP.NET web-site.
^^ .NET 4.0 ASP.NET web-site. I've tweaked the code for prior versions of .NET, I haven't tested it though.
|
2

I used this which can be called multiple times if you have a list of keys that you want removed from the querystring if they exists:

private string RemoveQueryStringItemWithKeyName(string url, string keyName)
{
    var urlItemRemoved = String.Join("&", url.Split('&').Where(k => !k.StartsWith(keyName + "=")));
    return urlItemRemoved;
}

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.