从一个字符串.NET URL参数字符串、参数、NET、URL

2023-09-02 01:18:17 作者:默ゎ

我有一个字符串在.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 类返回的NameValueCollection

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