0

I grab a url and I want to parse and store two sections of the url

User/Confirmation?=QVNERkFTREY=&code=MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==

So I want to start at (Confirmation?=) and stop at (&) and store the results

string = QVNERkFTREY

then for the second one I want to start at (&code=) and go to the end of the string and store that result

string = MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==

I was have tried a few different things

Uri myUri = new Uri(Request.Url.AbsoluteUri);
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("Confirmation?=");
string param2 = myUri.Query.Split();

Pretty sure I should be going a different route here but any help would be appreciate. I am going to continue to google search for now. I appreciate the help.

EDIT: I feel as though LINQ should be able to help me here..hmm

NitroFrost
  • 133
  • 4
  • 20

1 Answers1

2

I would use HttpUtility.ParseQueryString rather than try to parse the URL myself.

String val = "User/Confirmation?=QVNERkFTREY=&code=MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==";
System.Collections.Specialized.NameValueCollection parameters = System.Web.HttpUtility.ParseQueryString(val);
Console.Out.WriteLine(parameters[0]);  // QVNERkFTREY=
Console.Out.WriteLine(parameters.Get("code"); // MTAvMjMvMjAxMyAxMjowMDowMCBBTQ==

You will need to add System.Web.dll, which you can read about here: Cannot add System.Web.dll reference

Community
  • 1
  • 1
MxLDevs
  • 19,048
  • 36
  • 123
  • 194