.net Web服务不接受输入

.net Web服务不接受输入,.net,web-services,validation,.net,Web Services,Validation,我正在开发一个简单的.NET4.0WebService。我创建了一个方法,它接受字符串输入。我在调试模式下运行项目,以便在浏览器中打开一个页面,在其中输入输入并调用服务的方法。不幸的是,我遇到以下错误: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (xmlData="<?xml v

我正在开发一个简单的.NET4.0WebService。我创建了一个方法,它接受字符串输入。我在调试模式下运行项目,以便在浏览器中打开一个页面,在其中输入输入并调用服务的方法。不幸的是,我遇到以下错误:

System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (xmlData="<?xml version="1.0" ...").
   at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
   at System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, RequestValidationSource requestCollection)
   at System.Web.HttpRequest.get_Form()
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
我试着加上

 <pages validateRequest="false" />

转到web.config。它不起作用

我能做什么?

我找到了解决方案:

在.Net 4中,您必须在下添加以下行:

 <httpRuntime requestValidationType="MyService.CustomRequestValidator" />

CustomRequestValidator类是您必须自己添加的验证。然后只需重写bool IsValidRequestString()方法并返回true即可消除验证:

/// <summary>
/// Validates the input based on some custom rules
/// </summary>
public class CustomRequestValidator : RequestValidator
{
    /// <summary>
    /// Validates a string that contains HTTP request data.
    /// </summary>
    /// <param name="context">The context of the current request.</param>
    /// <param name="value">The HTTP request data to validate.</param>
    /// <param name="requestValidationSource">An enumeration that represents the source of request data that is being validated. The following are possible values for the enumeration:QueryStringForm CookiesFilesRawUrlPathPathInfoHeaders</param>
    /// <param name="collectionKey">The key in the request collection of the item to validate. This parameter is optional. This parameter is used if the data to validate is obtained from a collection. If the data to validate is not from a collection, <paramref name="collectionKey"/> can be null.</param>
    /// <param name="validationFailureIndex">When this method returns, indicates the zero-based starting point of the problematic or invalid text in the request collection. This parameter is passed uninitialized.</param>
    /// <returns>
    /// true if the string to be validated is valid; otherwise, false.
    /// </returns>
    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;

        return true;

        //    // All other HTTP input checks are left to the base ASP.NET implementation.
        //    return base.IsValidRequestString(
        //                                        context,
        //                                        value,
        //                                        requestValidationSource,
        //                                        collectionKey,
        //                                        out validationFailureIndex);            
    }
}
//
///根据一些自定义规则验证输入
/// 
公共类CustomRequestValidator:RequestValidator
{
/// 
///验证包含HTTP请求数据的字符串。
/// 
///当前请求的上下文。
///要验证的HTTP请求数据。
///表示正在验证的请求数据源的枚举。以下是枚举的可能值:QueryStringForm CookiesFilesRawUrlPathInfoHeaders
///要验证的项的请求集合中的键。此参数是可选的。如果要验证的数据来自集合,则使用此参数。如果要验证的数据不是来自集合,则可以为null。
///此方法返回时,指示请求集合中有问题或无效文本的从零开始的起点。此参数未初始化就传递。
/// 
///如果要验证的字符串有效,则为true;否则为false。
/// 
受保护的覆盖布尔IsValidRequestString(HttpContext上下文、字符串值、RequestValidationSource RequestValidationSource、string collectionKey、out int validationFailureIndex)
{
//设置out参数的默认值。
validationFailureIndex=-1;
返回true;
////所有其他HTTP输入检查由基本ASP.NET实现完成。
//return base.IsValidRequestString(
//背景,
//价值观,
//requestValidationSource,
//集合键,
//out validationFailureIndex);
}
}
}