如果未传递参数,如何避免vb.net中的请求查询字符串异常

the*_*mhz 2 .net vb.net asp.net

如果未传递参数变量,则此代码抛出"从字符串转换""到"类型'日期'无效的异常."

这是我的代码.

 Public Shared ReadOnly Property Request_projectStartDate() As Date
        Get
            Dim _value As String = Nothing

            If Not HttpContext.Current.Request.QueryString("projectStartDate") Is Nothing Then
                _value = HttpContext.Current.Request.QueryString("projectStartDate").ToString()
            End If

            Return CDate(_value)
        End Get
    End Property
Run Code Online (Sandbox Code Playgroud)

Kam*_*aiz 14

您可以查看@Massimiliano报告的内容以及另外一项检查

If Request.QueryString.HasKeys() Then

   // Check for specified querystrings...
   If Not String.IsNullOrEmpty(Request.QueryString("projectStartDate")) Then
       // Your logic
   End If

End If
Run Code Online (Sandbox Code Playgroud)