RequestValidationSource 列舉

定義

指定要驗證的 HTTP 要求資料類型。

public enum class RequestValidationSource
public enum RequestValidationSource
type RequestValidationSource = 
Public Enum RequestValidationSource
繼承
RequestValidationSource

欄位

Cookies 2

要求 Cookie。

Files 3

上載的檔案。

Form 1

表單值。

Headers 7

要求標頭。

Path 5

虛擬路徑。

PathInfo 6

HTTP PathInfo 字串,此字串為 URL 路徑的延伸部分。

QueryString 0

查詢字串。

RawUrl 4

未經處理的 URL (URL 中在網域之後的部分)。

範例

下列範例示範如何建立自訂要求驗證程式類別,該類別只允許查詢字串值的特定字串。

Imports System.Web  
Imports System.Web.Util  

Public Class CustomRequestValidation  
    Inherits RequestValidator  

Protected Overloads Overrides Function IsValidRequestString( _  
        ByVal context As HttpContext, _  
        ByVal value As String, _  
        ByVal requestValidationSource__1 As RequestValidationSource, _  
        ByVal collectionKey As String, _  
        ByRef validationFailureIndex As Integer) As Boolean  
    validationFailureIndex = -1  
    ' Set a default value for the out parameter.  
    ' This application does not use RawUrl directly, so you can   
    ' ignore the check for RequestValidationSource.RawUrl.  
    If requestValidationSource = RequestValidationSource.RawUrl Then  
        Return True  
    End If  

    ' Allow the query-string key "data" to have an XML-like value.  
    If (requestValidationSource = _  
            (RequestValidationSource.QueryString) AndAlso _  
            (collectionKey = "data") Then  
        ' The querystring value "<example>1234</example>" is allowed.  
        If value = "<example>1234</example>" Then  
            validationFailureIndex = -1  
            Return True  
        Else  
            ' Leave any further checks to ASP.NET.  
            Return MyBase.IsValidRequestString(context, value, _  
                requestValidationSource__1, collectionKey, _  
                validationFailureIndex)  
        End If  
    Else  
        ' All other HTTP input checks fall back to   
        ' the base ASP.NET implementation.  
        Return MyBase.IsValidRequestString(context, value, _  
            requestValidationSource__1, collectionKey, _  
            validationFailureIndex)  
    End If  
End Function  
End Class  
using System;  
using System.Web;  
using System.Web.Util;  

public class CustomRequestValidation : RequestValidator  
{  
    public CustomRequestValidation() {}  

    protected override bool IsValidRequestString(  
        HttpContext context, string value,   
        RequestValidationSource requestValidationSource, string collectionKey,   
        out int validationFailureIndex)  
    {  
        //Set a default value for the out parameter.  
        validationFailureIndex = -1;  

        // This application does not use RawUrl directly,   
        // so you can ignore the check for RequestValidationSource.RawUrl.  
        if (requestValidationSource == RequestValidationSource.RawUrl)  
            return true;  

        // Allow the query-string key "data" to have an XML-like value.  
        if (  
            (requestValidationSource == RequestValidationSource.QueryString) &&  
            (collectionKey == "data")  
           )  
        {  
            // The querystring value "<example>1234</example>" is allowed.  
            if (value == "<example>1234</example>")  
            {  
                validationFailureIndex = -1;  
                return true;  
            }  
            else  
           // Leave any further checks to ASP.NET.  
                return base.IsValidRequestString(context, value,   
                requestValidationSource, collectionKey, out   
                validationFailureIndex);  
        }  
        // All other HTTP input checks fall back to   
        // the base ASP.NET implementation.  
        else  
        {  
            return base.IsValidRequestString(context, value,   
                requestValidationSource, collectionKey,   
                out validationFailureIndex);  
        }  
    }  
}  

下列範例顯示如何設定 ASP.NET 使用自訂驗證程式。

<httpRuntime requestValidationType="CustomRequestValidation" />  

備註

您可以藉由執行類型來建立自訂要求驗證類型 RequestValidator 。 當 ASP.NET 呼叫 IsValidRequestString 方法來驗證要求時,ASP.NET 會傳遞 requestValidationSource 參數,以指定要驗證之資料的來源。 RequestValidationSource列舉是用來指定要驗證之要求資料的來源或類型。 列舉型別(enumeration)指出在方法的參數中傳遞的 HTTP 輸入型別 value IsValidRequestString 。 如果您不想要使用自訂邏輯進行驗證,可以使用列舉來切換回 HTTP 輸入的基底要求驗證執行。

下表說明如何 collectionKey value RequestValidator.IsValidRequestString 針對列舉的每個成員來解讀方法的和參數值 RequestValidationSource

列舉成員 collectionKey 參數 value 參數
Cookies 集合中 cookie 的名稱。 集合中的值。
Files 集合中已上傳檔案的名稱。 集合中已上傳檔案的值。
Form 集合中表單參數的名稱。 集合中 form 參數的值。
Headers 集合中 HTTP 標頭的名稱。 集合中的 HTTP 標頭值。
Path null (Path 不是) 值的集合。 [路徑] 欄位的值。
PathInfo null (PathInfo 不是) 值的集合。 System.management.automation.pathinfo 欄位的值。
QueryString 集合中查詢字串參數的名稱。 集合中查詢字串參數的值。
RawUrl null (RawUrl 不是值的集合。 ) RawUrl 欄位的值。

適用於

另請參閱