Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the parameter from a relative URL string in C#?

What's the most efficient way to get a specific parameter from a relative URL string using C#?

For example, how would you get the value of the ACTION parameter from the following relative URL string:

string url = "/page/example?ACTION=data&FOO=test";

I have already tried using:

var myUri = new Uri(url, UriKind.Relative);
String action = HttpUtility.ParseQueryString(myUri.Query).Get("ACTION");

However, I get the following error:

This operation is not supported for a relative URI.

like image 635
Daniel Congrove Avatar asked Jul 28 '16 19:07

Daniel Congrove


1 Answers

 int idx = url.IndexOf('?');
 string query = idx >= 0 ? url.Substring(idx) : "";
 HttpUtility.ParseQueryString(query).Get("ACTION");
like image 121
alexm Avatar answered Sep 28 '22 12:09

alexm