从当前URL获取字符串

时间:2014-01-11 17:48:21

标签: asp.net string url substring

我正在编写一个asp.net MVC应用程序。我让应用程序向FreeAgent发送请求,如果请求成功,则在URL的重定向中返回代码。 例如,这是成功URL的副本。

{
http://localhost:3425/FreeAgent/Home?code=144B2ymEKw3JfB9EDPIqCGeWKYLb9IKc-ABI6SZ0o&state=
}

他们已将?code = 144B2ymEKw3JfB9EDPIqCGeWKYLb9IKc-ABI6SZ0o& state =添加到我的网址 我需要在?code =之后和& state =

之前的位

我可以用它来获取网址

string code = Request.Url.AbsoluteUri;

但我需要帮助从此

中提取代码

编辑: 每次运行时代码都不同

1 个答案:

答案 0 :(得分:2)

您可以使用System.UriSystem.Web.HttpUtility

string uri = "http://localhost:3425/FreeAgent/Home?code=144B2ymEKw3JfB9EDPIqCGeWKYLb9IKc-ABI6SZ0o&state=";
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);

然后code

中将显示queryDictionary["code"]查询参数的值