1

I have an application where uses post comments. Security is not an issue. string url = http://example.com/xyz/xyz.html?userid=xyz&comment=Comment

What i want is to extract the userid and comment from above string. I tried and found that i can use IndexOf and Substring to get the desired code BUT what if the userid or comment also has = symbol and & symbol then my IndexOf will return number and my Substring will be wrong. Can you please find me a more suitable way of extracting userid and comment. Thanks.

Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
  • No. You can get them from the Response object, I believe, as query string values. –  Jun 05 '15 at 14:46
  • possible duplicate of [Get url parameters from a string in .NET](http://stackoverflow.com/questions/659887/get-url-parameters-from-a-string-in-net) – Markus Jun 05 '15 at 14:50

2 Answers2

5

I got url using string url = HttpContext.Current.Request.Url.AbsoluteUri;

Do not use AbsoluteUri property , it will give you a string Uri, instead use the Url property directly like:

var result = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.Url.Query);

and then you can extract each parameter like:

Console.WriteLine(result["userid"]);
Console.WriteLine(result["comment"]);

For other cases when you have string uri then do not use string operations, instead use Uri class.

Uri uri = new Uri(@"http://example.com/xyz/xyz.html?userid=xyz&comment=Comment");

You can also use TryCreate method which doesn't throw exception in case of invalid Uri.

Uri uri;
if (!Uri.TryCreate(@"http://example.com/xyz/xyz.html?userid=xyz&comment=Comment", UriKind.RelativeOrAbsolute, out uri))
{
    //Invalid Uri
}

and then you can use System.Web.HttpUtility.ParseQueryString to get query string parameters:

 var result = System.Web.HttpUtility.ParseQueryString(uri.Query);
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I am newbie, can you please suggest how can i extract xyz and Comment from the above link after using uri – Dr. Mian Jun 05 '15 at 14:47
  • I got url using string url = HttpContext.Current.Request.Url.AbsoluteUri; Now i do Uri uri = new Uri(@url); var result = System.Web.HttpUtility.ParseQueryString(uri.Query); – Dr. Mian Jun 05 '15 at 14:49
  • Remember that [IndexOf](https://msdn.microsoft.com/en-us/library/k8b1470s%28v=vs.110%29.aspx) is always there when you need it. – yazanpro Jun 05 '15 at 14:50
  • @Asbat, thats great, do not use `AbsoluteUri`, instead use `Url` directly, without converting it to string. – Habib Jun 05 '15 at 14:50
  • @Asbat, I have modified the answer – Habib Jun 05 '15 at 14:54
  • Thanks but it looks more complicated to me now :) I tried it before and it worked like a charm. instead of console i used response to see the values on webpage when testing. Great. – Dr. Mian Jun 05 '15 at 15:08
  • Can you comment on my another question, I am just asking please. thanks. – Dr. Mian Jun 05 '15 at 15:11
  • @Asbat, what other question ? I see that this is your latest question on SO – Habib Jun 05 '15 at 15:14
0

The ugliest way is the following:

String url = "http://example.com/xyz/xyz.html?userid=xyz&comment=Comment";
usr = url.Split('?')[1];
usr= usr.Split('&')[0];
usr = usr.Split('=')[1];

But @habib version is better