从 .NET 中的字符串获取 URL 参数
我在 .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"]
,但是这个字符串不是来自请求.我可以像这样创建一个新的 Uri
项目:
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
来获取查询字符串...但是我显然必须找到一些正则表达式来拆分它.
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?
使用返回 NameValueCollection
System.Web.HttpUtility 类的静态 ParseQueryString
方法代码>.
Use static ParseQueryString
method of System.Web.HttpUtility
class that returns NameValueCollection
.
Uri myUri = new Uri("http://www.example.com?param1=good¶m2=bad");
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1");
在 http://msdn.microsoft.com/en-us 查看文档/library/ms150046.aspx