RequestValidationSource 枚举

定义

指定要验证的 HTTP 请求数据类型。

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

字段

名称 说明
QueryString 0

查询字符串。

Form 1

表单值。

Cookies 2

请求 Cookie。

Files 3

上传的文件。

RawUrl 4

原始 URL。 (域后 URL 的一部分。

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 用于指定正在验证的源或请求数据的种类。 枚举指示在方法的参数中 value 传递的 IsValidRequestString HTTP 输入的类型。 如果不想使用自定义逻辑进行验证,可以使用枚举来回退到 HTTP 输入的基本请求验证实现。

下表显示了如何为枚举的每个成员collectionKey解释方法的值valueRequestValidator.IsValidRequestString参数RequestValidationSource

枚举成员 collectionKey 参数 value 参数
Cookies 集合中 Cookie 的名称。 集合中的值。
Files 集合中上传的文件的名称。 集合中上传的文件的值。
Form 集合中窗体参数的名称 集合中窗体参数的值。
Headers 集合中 HTTP 标头的名称。 集合中 HTTP 标头的值。
Path nullPath 不是值的集合)。 “路径”字段的值。
PathInfo nullPathInfo 不是值的集合)。 PathInfo 字段的值。
QueryString 集合中查询字符串参数的名称。 集合中查询字符串参数的值。
RawUrl nullRawUrl 不是值的集合。 RawUrl 字段的值。

适用于

另请参阅