从.NET中的string获取url参数

我在.NET中有一个string,它实际上是一个URL。 我想要一个简单的方法来获取特定参数的值。

通常情况下,我只是使用Request.Params["theThingIWant"] ,但是这个string不是来自请求。 我可以创build一个新的Uri项目,如下所示:

 Uri myUri = new Uri(TheStringUrlIWantMyValueFrom); 

我可以使用myUri.Query来获取查询string…但是,我显然必须find一些合理的分裂方式。

我是否错过了一些显而易见的东西,还是没有build立一个这样的创build正则expression式的方法?

使用返回NameValueCollectionSystem.Web.HttpUtility类的静态ParseQueryString方法。

 Uri myUri = new Uri("http://www.example.com?param1=good&param2=bad"); string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1"); 

检查文档http://msdn.microsoft.com/en-us/library/ms150046.aspx

这可能是你想要的

 var uri = new Uri("http://domain.test/Default.aspx?var1=true&var2=test&var3=3"); var query = HttpUtility.ParseQueryString(uri.Query); var var2 = query.Get("var2"); 

如果出于任何原因,您不能或不想使用HttpUtility.ParseQueryString() ,这是另一种select。

这是build立在一定程度上容忍“畸形”的查询string,即http://test/test.html?empty=成为一个空值的参数。 调用者可以根据需要validation参数。

 public static class UriHelper { public static Dictionary<string, string> DecodeQueryParameters(this Uri uri) { if (uri == null) throw new ArgumentNullException("uri"); if (uri.Query.Length == 0) return new Dictionary<string, string>(); return uri.Query.TrimStart('?') .Split(new[] { '&', ';' }, StringSplitOptions.RemoveEmptyEntries) .Select(parameter => parameter.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries)) .GroupBy(parts => parts[0], parts => parts.Length > 2 ? string.Join("=", parts, 1, parts.Length - 1) : (parts.Length > 1 ? parts[1] : "")) .ToDictionary(grouping => grouping.Key, grouping => string.Join(",", grouping)); } } 

testing

 [TestClass] public class UriHelperTest { [TestMethod] public void DecodeQueryParameters() { DecodeQueryParametersTest("http://test/test.html", new Dictionary<string, string>()); DecodeQueryParametersTest("http://test/test.html?", new Dictionary<string, string>()); DecodeQueryParametersTest("http://test/test.html?key=bla/blub.xml", new Dictionary<string, string> { { "key", "bla/blub.xml" } }); DecodeQueryParametersTest("http://test/test.html?eins=1&zwei=2", new Dictionary<string, string> { { "eins", "1" }, { "zwei", "2" } }); DecodeQueryParametersTest("http://test/test.html?empty", new Dictionary<string, string> { { "empty", "" } }); DecodeQueryParametersTest("http://test/test.html?empty=", new Dictionary<string, string> { { "empty", "" } }); DecodeQueryParametersTest("http://test/test.html?key=1&", new Dictionary<string, string> { { "key", "1" } }); DecodeQueryParametersTest("http://test/test.html?key=value?&b=c", new Dictionary<string, string> { { "key", "value?" }, { "b", "c" } }); DecodeQueryParametersTest("http://test/test.html?key=value=what", new Dictionary<string, string> { { "key", "value=what" } }); DecodeQueryParametersTest("http://www.google.com/search?q=energy+edge&rls=com.microsoft:en-au&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1%22", new Dictionary<string, string> { { "q", "energy+edge" }, { "rls", "com.microsoft:en-au" }, { "ie", "UTF-8" }, { "oe", "UTF-8" }, { "startIndex", "" }, { "startPage", "1%22" }, }); DecodeQueryParametersTest("http://test/test.html?key=value;key=anotherValue", new Dictionary<string, string> { { "key", "value,anotherValue" } }); } private static void DecodeQueryParametersTest(string uri, Dictionary<string, string> expected) { Dictionary<string, string> parameters = new Uri(uri).DecodeQueryParameters(); Assert.AreEqual(expected.Count, parameters.Count, "Wrong parameter count. Uri: {0}", uri); foreach (var key in expected.Keys) { Assert.IsTrue(parameters.ContainsKey(key), "Missing parameter key {0}. Uri: {1}", key, uri); Assert.AreEqual(expected[key], parameters[key], "Wrong parameter value for {0}. Uri: {1}", parameters[key], uri); } } } 

看起来你应该循环myUri.Query的值,并从那里parsing它。

  string desiredValue; foreach(string item in myUri.Query.Split('&')) { string[] parts = item.Replace('?', '').Split('='); if(parts[0] == "desiredKey") { desiredValue = parts[1]; break; } } 

但是,如果不对一些格式不正确的URL进行testing,我不会使用此代码。 它可能会打破一些/所有这些:

  • hello.html?
  • hello.html?valuelesskey
  • hello.html?key=value=hi
  • hello.html?hi=value?&b=c
  • 等等

@Andrew和@CZFox

我有同样的错误,发现原因是参数之一,其实是: http://www.example.com?param1 : http://www.example.com?param1 param1而不是param1这是人们所期望的。

通过删除所有字符,并包括问号修复此问题。 所以实质上HttpUtility.ParseQueryString函数只需要一个有效的查询string参数,其中只包含问号之后的字符,如下所示:

 HttpUtility.ParseQueryString ( "param1=good&param2=bad" ) 

我的解决方法:

 string RawUrl = "http://www.example.com?param1=good&param2=bad"; int index = RawUrl.IndexOf ( "?" ); if ( index > 0 ) RawUrl = RawUrl.Substring ( index ).Remove ( 0, 1 ); Uri myUri = new Uri( RawUrl, UriKind.RelativeOrAbsolute); string param1 = HttpUtility.ParseQueryString( myUri.Query ).Get( "param1" );` 

您也可以使用以下解决方法来处理第一个参数:

 var param1 = HttpUtility.ParseQueryString(url.Substring( new []{0, url.IndexOf('?')}.Max() )).Get("param1"); 

使用.NET Reflector查看System.Web.HttpValueCollectionFillFromString方法。 这为您提供了ASP.NET正在使用的代码来填充Request.QueryString集合。

 HttpContext.Current.Request.QueryString.Get("id"); 

或者如果您不知道URL(为了避免硬编码,请使用AbsoluteUri

例子…

  //get the full URL Uri myUri = new Uri(Request.Url.AbsoluteUri); //get any parameters string strStatus = HttpUtility.ParseQueryString(myUri.Query).Get("status"); string strMsg = HttpUtility.ParseQueryString(myUri.Query).Get("message"); switch (strStatus.ToUpper()) { case "OK": webMessageBox.Show("EMAILS SENT!"); break; case "ER": webMessageBox.Show("EMAILS SENT, BUT ... " + strMsg); break; } 

如果你想让你的QueryString的默认页面。默认页面意味着你当前的网页url。 你可以试试这个代码:

 string paramIl = HttpUtility.ParseQueryString(this.ClientQueryString).Get("city"); 

我用它,它运行完美

 <%=Request.QueryString["id"] %>