从一个字符串.NET URL参数 [英] Get url parameters from a string in .NET

查看:186
本文介绍了从一个字符串.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] ,但此字符串不是来自请求。我可以创建一个新的乌里项目像这样:

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");

在<一检查文档href="http://msdn.microsoft.com/en-us/library/ms150046.aspx">http://msdn.microsoft.com/en-us/library/ms150046.aspx

这篇关于从一个字符串.NET URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆