RequestValidationSource 列舉

定義

指定要驗證的 HTTP 要求數據種類。

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

欄位

名稱 Description
QueryString 0

查詢字串。

Form 1

表單的數值。

Cookies 2

請求的 Cookies。

Files 3

上傳的檔案。

RawUrl 4

原始網址。 (網址中網域後的部分。)

Path 5

虛擬路徑。

PathInfo 6

HTTP PathInfo 字串,是 URL 路徑的擴充。

Headers 7

請求標頭。

範例

以下範例展示如何建立一個自訂的請求驗證器類別,該類別只允許查詢字串值使用特定字串。

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列舉用來指定被驗證的請求來源或類型。 列舉表示方法參數中傳遞valueIsValidRequestString的 HTTP 輸入類型。 如果你不想用自訂邏輯驗證,可以用列舉作為 HTTP 輸入的基礎請求驗證實作。

下表展示了方法中 和 collectionKeyvalue 參數 RequestValidator.IsValidRequestString 值在每個枚舉成員 RequestValidationSource 下的解釋方式。

列舉成員 collectionKey 參數 value 參數
Cookies 收藏裡那塊餅乾的名字。 收藏的價值。
Files 集合中上傳檔案的名稱。 集合中上傳檔案的價值。
Form 集合中形式參數的名稱 集合中 form 參數的值。
Headers 集合中 HTTP 標頭的名稱。 集合中 HTTP 標頭的值。
Path nullPath 並非一組數值的集合)。 路徑欄位的價值。
PathInfo nullPathInfo 並非一組數值的集合)。 PathInfo 欄位的價值。
QueryString 集合中查詢字串參數的名稱。 集合中查詢字串參數的值。
RawUrl nullRawUrl 並非一組數值。) RawUrl 欄位的價值。

適用於

另請參閱