从一个字符串.NET URL参数 [英] Get url parameters from a string in .NET
问题描述
我有一个字符串在.NET中这实际上是一个URL。我希望有一个简单的方法来从一个特定的参数的值。
I've got a string in .NET which is actually a url. I want an easy way to get the value from a particular parameter.
通常情况下,我只是使用 Request.Params [theThingIWant]
,但此字符串不是来自请求。我可以创建一个新的乌里
项目像这样:
Normally, I'd just use Request.Params["theThingIWant"]
, but this string isn't from the request. I can create a new Uri
item like so:
Uri myUri = new Uri(TheStringUrlIWantMyValueFrom);
我可以使用 myUri.Query
获取查询字符串...但我显然必须要找到将其分割了一些regexy方式。
I can use myUri.Query
to get the query string...but then I apparently have to find some regexy way of splitting it up.
我失去了一些东西明显,或者是有没有内置的方式做创建一个正则表达式了一番,等这短短的?
Am I missing something obvious, or is there no built in way to do this short of creating a regex of some kind, etc?
推荐答案
使用静态 ParseQueryString
System.Web.HttpUtility $ C的方法$ C>类返回
的NameValueCollection
。
Uri myUri = new Uri("http://www.example.com?param1=good¶m2=bad");
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1");
在<一检查文档href="http://msdn.microsoft.com/en-us/library/ms150046.aspx">http://msdn.microsoft.com/en-us/library/ms150046.aspx
这篇关于从一个字符串.NET URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!