RequestValidationSource Enumerazione

Definizione

Specifica il tipo di dati della richiesta HTTP da convalidare.

public enum class RequestValidationSource
public enum RequestValidationSource
type RequestValidationSource = 
Public Enum RequestValidationSource
Ereditarietà
RequestValidationSource

Campi

Nome Valore Descrizione
QueryString 0

Stringa di query.

Form 1

Valori del modulo.

Cookies 2

Cookie di richiesta.

Files 3

File caricato.

RawUrl 4

URL non elaborato. (Parte di un URL dopo il dominio).

Path 5

Percorso virtuale.

PathInfo 6

Stringa HTTP PathInfo , che è un'estensione di un percorso URL.

Headers 7

Intestazioni delle richieste.

Esempio

Nell'esempio seguente viene illustrato come creare una classe di validator di richiesta personalizzata che consenta solo una stringa specifica per i valori di stringa di query.

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);
        }
    }
}

Nell'esempio seguente viene illustrato come configurare ASP.NET per l'uso del validator personalizzato.

<httpRuntime requestValidationType="CustomRequestValidation" />

Commenti

È possibile creare un tipo di convalida di richiesta personalizzato implementando il RequestValidator tipo . Quando ASP.NET chiama il metodo per convalidare una richiesta, ASP.NET passa un parametro /> RequestValidationSource viene utilizzata per specificare l'origine o il tipo di dati della richiesta da convalidare. L'enumerazione indica il tipo di input HTTP passato nel value parametro del IsValidRequestString metodo . È possibile usare l'enumerazione come metodo per eseguire il fallback all'implementazione di convalida della richiesta di base per gli input HTTP se non si vuole convalidare usando la logica personalizzata.

Nella tabella seguente viene illustrato il modo in cui il valore del collectionKey parametro e value del RequestValidator.IsValidRequestString metodo viene interpretato per ogni membro dell'enumerazione RequestValidationSource .

Membro di enumerazione parametro collectionKey parametro value
Cookies Nome del cookie nella raccolta. Valore nell'insieme.
Files Nome del file caricato nella raccolta. Valore del file caricato nella raccolta.
Form Nome del parametro del modulo nella raccolta Valore del parametro form nell'insieme.
Headers Nome di un'intestazione HTTP nella raccolta. Valore dell'intestazione HTTP nella raccolta.
Path null (Path non è una raccolta di valori). Valore del campo Path.
PathInfo null (PathInfo non è una raccolta di valori). Valore del campo PathInfo.
QueryString Nome del parametro della stringa di query nella raccolta. Valore del parametro della stringa di query nella raccolta.
RawUrl null (RawUrl non è una raccolta di valori. Valore del campo RawUrl.

Si applica a

Vedi anche