从 .NET 中的字符串获取 URL 参数 [英] Get URL parameters from a string in .NET

查看:36
本文介绍了从 .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?

推荐答案

使用返回 NameValueCollectionSystem.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&param2=bad");
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1");

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

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

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