RequestValidator Class

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Defines base methods for custom request validation.

Namespace:   System.Web.Util
Assembly:  System.Web (in System.Web.dll)


public class RequestValidator


Initializes a new instance of the RequestValidator class.


Gets or sets a reference to the current RequestValidator instance that will be used in an application.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInvokeIsValidRequestString(HttpContext, String, RequestValidationSource, String, Int32)

Provides a public method that calls the protected IsValidRequestString method in order to validate HTTP request data.

System_CAPS_protmethodIsValidRequestString(HttpContext, String, RequestValidationSource, String, Int32)

Validates a string that contains HTTP request data.


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

By default, ASP.NET does not validate requests until code explicitly requests a value from the request. For example, ASP.NET does not validate query-string values until code accesses the QueryString collection. By default, ASP.NET also does not validate some types of request data, such as form values, cookies, the names of files that have been uploaded using HTTP, and the value of the RawUrl property.

The RequestValidator class is a base class that you can implement in order to provide custom request validation. By implementing this class, you can determine when validation occurs and what type of request data to perform validation on.

By default, ASP.NET provides cross-site scripting (XSS) checks. However, you can supplement or replace the request validation logic that is provided in ASP.NET by creating a custom implementation of the XSS. For example, you can write a custom request validation implementation that scans for SQL injection attacks in addition to checking for XSS attacks.

To create custom request validation, you write a custom class that derives from the RequestValidator base class. You then configure ASP.NET to use the custom request validator in the application-level Web.config file. You can put the custom class in the App_Code folder, in a compiled class library in the Bin folder, or in a compiled class library in the GAC.


Only one custom request validation type can be configured for an application. It is not possible to configure a different request validation type for individual virtual paths or pages.

The following example shows how to create a custom request validator that allows only a specific string to be used for query-string values.

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)
        validationFailureIndex = -1;  //Set a default value for the out parameter.

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

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

The following example shows how to configure ASP.NET to use the custom validator in the Web.config file for an application.

  <httpRuntime requestValidationType="CustomRequestValidation" />

.NET Framework
Available since 4.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top