从C#中的URL提取QueryParameter



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

输出:

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

我假设这里的returnUrl编码错误,在实际代码中是url编码的;在这种情况下,您需要第二次解析返回的url参数,但是-类似于:

var uri = new Uri("http://localhost:3000/Login/SignIn?timeout=5000&returnUrl=%2FHome.ashx%3Fparam1%3D555%26param2%3D666");
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]}");
}

输出:

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

相关内容

  • 没有找到相关文章

最新更新