ASP.Net - 未经验证的请求例外?

时间:2016-08-18 21:04:32

标签: asp.net exception request

环境:

Windows 2008 R2 Server
IIS 7.5
ASP.Net 4.5.1
web.config <httpRuntime requestValidationMode="2.0" />

我有一个函数,用于从查询字符串,表单或cookie集合中返回一个Integer ID。 99%的时间它工作得很好,下面是使用的代码:

Function get_id() as Integer

Const k_fld_name as String = "id"

Dim ok As Boolean

Dim id as Integer

Dim fld_val As String

Dim cookie as HttpCookie

Dim req As System.Web.HttpRequest


' Attempt to get ID ...

id = 0

fld_val = Nothing


' Get current request (if any) ...
' NOTE: https://msdn.microsoft.com/en-us/library/system.web.httpcontext.request(v=vs.110).aspx
' ASP.NET will throw an exception if you try to use this property when the HttpRequest object 
' Is Not available. For example, this would be true in the Application_Start method of the Global.asax file, 
' Or in a method that Is called from the Application_Start method. At that time no HTTP request 
' has been created yet ...

Try

   req = System.Web.HttpContext.Current.Request

Catch

   req = Nothing

End Try

If ( req Is Nothing ) Then

   Return id

End If


Try

'  Check in querystring ...

   fld_val = req.Unvalidated.QueryString( k_fld_name )

'  If not found, check in form ...

   If ( String.IsNullOrEmpty( fld_val ) ) Then

      fld_val = req.Unvalidated.Form( k_fld_name )

'     If not found, check in cookie ...

      If ( String.IsNullOrEmpty( fld_val ) ) Then

         cookie = req.Unvalidated.Cookies( k_fld_name )

         If ( cookie isNot Nothing ) Then

            fld_val = cookie.Value

         End If

      End If

   End If

Catch ex As Exception

   error_log( ex.ToString() )

End Try

[ other logic to convert to Integer ]

Return id

End Function

但是经常会记录以下异常:

System.Web.HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x800703E3. ---> System.NotSupportedException: Specified method is not supported.
   at System.Web.HttpResponseStream.get_Position()
   at System.Drawing.UnsafeNativeMethods.ComStreamFromDataStream.Seek(Int64 offset, Int32 origin)
   at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
   at System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size)
   at System.Web.Hosting.IIS7WorkerRequest.ReadEntityBody(Byte[] buffer, Int32 size)
   at System.Web.HttpRequest.GetEntireRawContent()
   at System.Web.HttpRequest.FillInFormCollection()
   at System.Web.HttpRequest.EnsureForm()
   at System.Web.UnvalidatedRequestValues.get_Form()

为什么System.Drawing会出现在我身边。我想知道根据异常消息不支持WHAT方法吗?

我是否需要检查.Unvalidated属性是否为null以及任何其他集合,即:

If ( req.Unvalidated isNot Nothing ) Then
   ...

   If ( req.Unvalidated.Form isNot Nothing ) Then

     ...

   End If

End If

不幸的是,MS文档没有说明上述属性是否总是存在(有值)(是否为空)。

任何建议或想法都会受到赞赏,因为我反对这一点。

提前致谢。

1 个答案:

答案 0 :(得分:0)

ValidateRequest="false"放入您的aspx页面

<% ValidateRequest="false"%> 

然后在您的代码文件中使用

Request.Unvalidated.Form["yourrequest"];