2016-08-18 88 views
0

環境:ASP.Net - 未驗證的請求異常?

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

我有一個用於從查詢字符串,形式或Cookies集合返回一個整數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中被顯示出來超越了我。我想知道根據異常消息不支持什麼方法?

我需要檢查,如果.Unvalidated屬性不爲null,以及任何其他的集合,即:

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

    If (req.Unvalidated.Form isNot Nothing) Then 

    ... 

    End If 

End If 

不幸的是,MS文檔沒有說明上述性質是否是始終存在(有一個值)或不是(是空的)。

任何建議或想法,將不勝感激,因爲我對這一個磚牆。

在此先感謝。

回答

0

在你的aspx頁面將ValidateRequest="false"

<% ValidateRequest="false"%> 

然後在你的代碼文件中使用

Request.Unvalidated.Form["yourrequest"];