在[WebMethod]中捕获HttpRequestValidationException时返回字符串

时间:2015-01-13 12:37:15

标签: .net web-services try-catch asmx webmethod

所以,我正在尝试隐藏HttpRequestValidationException的错误。

这是我的WebMethod:

[WebMethod]
public string ToLower(string inputString)
{
    try
    {
        return inputString.ToLower();
    }
    catch (HttpRequestValidationException)
    {
        return "[INVALID]";           
    }
    catch (Exception ex)
    {
        return String.Format("Something went wrong-> {0}", ex);
    }
}

当有人抓住HttpRequestValidationException时,有没有人知道它为什么不返回“[INVALID]”? 还有什么是避免显示如下错误的最佳方法:

System.Web.HttpRequestValidationException:从客户端检测到潜在危险的Request.Form值(inputString =“<!doctype html>< htm ...”)。    at System.Web.HttpRequest.ValidateString(String value,String collectionKey,RequestValidationSource requestCollection)    在System.Web.HttpRequest。<> c__DisplayClass5.b__3(字符串键,字符串值)    在System.Web.HttpValueCollection.EnsureKeyValidated(String key)    在System.Web.HttpValueCollection.Get(String name)    在System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection集合)    在System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest请求)    在System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()    在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

0 个答案:

没有答案
相关问题