Home > Back-end >  QueryParameter extract from URL in C#
QueryParameter extract from URL in C#

Time:10-07

Url: http://localhost:3000/Login/SignIn?timeout=5000&returnUrl=/Home.ashx?param1=555&param2=666

Output:

{
    [0]: { timeout, 3000 },
    [1]: { param1, 555 },
    [2]: { param2, 666 }
}

CodePudding user response:

I'm assuming that the returnUrl here is incorrectly encoded, and in the real code would be url-encoded; in that case, you need a second pass to parse the return-url parameters, but - something like:

var uri = new Uri("http://localhost:3000/Login/SignIn?timeout=5000&returnUrl=/Home.ashx?param1=555¶m2=666");
var query = HttpUtility.ParseQueryString(uri.Query);
foreach (string key in query.Keys)
{
    Console.WriteLine($"{key}={query[key]}");
}
// further-decode returnUrl values
uri = new Uri(uri, query["returnUrl"]);
query = HttpUtility.ParseQueryString(uri.Query);
foreach (string key in query.Keys)
{
    Console.WriteLine($"{key}={query[key]}");
}

which outputs:

timeout=5000
returnUrl=/Home.ashx?param1=555&param2=666
param1=555
param2=666
  • Related