Request.QueryString 不能这样使用

Request.QueryString cannot be used in this way

我正在尝试使用第二个函数从 Url 中检索值。第一个函数函数传入 checkedValue 变量,第二个函数应该检查类别是否存在,如果它是 return 值。

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>

我已经在互联网上搜索过,但一无所获,感谢任何帮助或建议

让您的实用程序class将请求作为参数而不是从页面继承:

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 on the HttpRequest class 以便您可以像这样使用它:

Request.CheckString("stock")