tags:

views:

248

answers:

3

I need to strip out any "&id=SomeValue" from a Url.PathAndQuery. Where SomeValue could be an int or a string. And it may or may not be followed by another ampersand.

So it could be

somepage.aspx?cat=22&id=SomeId&param2=4

or

somepage.aspx?cat=tect&id=450

I want to be left with

somepage.aspx?cat=22&param2=4

or

somepage.aspx?cat=tect
+3  A: 
roufamatic
This code does not compile, nor does it produce the correct output if the compilation errors are fixed. +1 because there are good ideas in the pseudocode.
binarycoder
ok, now it runs.
roufamatic
A: 

If bad things could happen (e.g., security-wise) if an "id=" parameter were missed by the regular expression, then you also need to worry that the query string might contain a hexadecimal urlencoded equivalent, which the regular expression will not recognize. For example, "id" is "%69%64". Also consider the effects of different capitalizations of "id" on your program. My opinion in this situtation is that you read the RFCs and build a complete class that can do transformations in both directions from a set of name-value pairs to a query strings. System.Uri will not do this. if you are running inside an ASP.NET application, you might investigate if HttpUtility.ParseQueryString is sufficient.

binarycoder
A: 

I would first parse the Querystring to Strongly typed values, then I would check using Regex if I needed to.

http://stackoverflow.com/questions/574868/c-asp-net-querystring-parser

JeremySpouken