1

事实证明,Asp.net 比 PHP 更容易使用(到目前为止)。但是,我一直在寻找一段时间,根本无法弄清楚这一点。如何获取页面 url 中包含的变量(源自具有“GET”方法的表单)并使用它们?

例如,我的页面是 www.example.com/index.asp?somevariable=something

我将如何获得某个变量的值?

4

5 回答 5

3

您可以使用 ybo 所说的,但它并不完整(至少对于 VB)。仅此一项就可能引发空引用异常。您想要转换(即 TryParse)这些值,并处理您希望包含值的任何空参数:

Dim itemId As Integer
Dim itemType as String

If Not Integer.TryParse(Request.QueryString("i").ToString, itemId) Then
    itemId = -1 ' Or whatever your default value is
Else
    ' Else not required. Variable itemId contains the value when Integer.TryParse returns True.
End If

itemType = Request.QueryString("t").ToString ' <-- ToString important here!
于 2009-03-14T15:17:36.047 回答
2

这很简单:

Request.QueryString["somevariable"]; // C#
Request.QueryString("somevariable") ' VB
于 2009-03-14T15:10:53.723 回答
0

这适用于 ASPX c#:

NameValueCollection pColl = Request.Params;
if (pColl["somevariable"] != null)
{
    string yourvalue = pColl["somevariable"];
}
于 2009-03-14T15:14:02.607 回答
0

任何像样的 HTTP 框架或库或软件,脚本/本机/托管/路由,都可以为您烹饪/分解/破解 URL 组件。

Request.QueryString 是一种古老的处理方式。查找 Uri 模板机制或新的 MVC 位。你迟早会需要它。

于 2009-03-14T16:00:26.177 回答
0
NameValueCollection col1 = Request.Query;
name=col1.GetValues("somevariable")[0].ToString();
于 2009-09-17T12:50:30.253 回答