Request.QueryString不能以这种方式使用

时间:2015-12-29 16:58:24

标签: asp.net vb.net httprequest

我正在尝试使用第二个函数从Url中检索值。第一个函数函数传入checkedValue变量,第二个函数函数应检查类别是否存在以及是否返回值。

Imports System.Web.HttpRequest

Public Class ReviewPageDefault
Inherits Page

Shared Function GetProductId()

    Dim util As New Utilities

    Dim product = ""

    If util.CheckString("schoolid") = "" And util.CheckString("stockid") = "" Then
        product = (util.CheckString("stock"))
    ElseIf util.CheckString("stock") = "" And util.CheckString("stockid") = "" Then
        product = (util.CheckString("FN"))
    Else
        Dim stockId = util.CheckString("stockid")
        product = stockId
    End If
    Return product

End Function

End Class

Public Class Utilities
Inherits Page

Public Function CheckString(checkedValue As String)

    Dim check = ""
    If Request.QueryString(checkedValue) Is Nothing Then
    Else
        check = Request.QueryString(checkedValue)
    End If
    Return check

End Function

End Class

然而,每当我尝试测试页面时,我都会收到错误

System.Web.HttpException: Request is not available in this context

代码位于asp.net页面的代码后面,该页面试图检索产品值

<script>
    var product = '<%= ReviewPageDefault.GetProductId()%>';
</script>

我在互联网上搜索过但没有找到任何帮助或建议表示赞赏

1 个答案:

答案 0 :(得分:1)

让您的Utilities类将Request作为参数而不是继承自Page:

Public Class Utilities

    Public Function CheckString(checkedValue As String, request as HttpRequest)

        Dim check = ""
        If request.QueryString(checkedValue) Is Nothing Then
        Else
            check = request.QueryString(checkedValue)
        End If
        Return check

    End Function

End Class

并在调用时从主页面传递请求:

util.CheckString("stock", Request)

或在extension method类上设为HttpRequest,以便您可以像这样使用它:

Request.CheckString("stock")