处理"潜在危险的Request.Form值......"

key*_*rdP 14 asp.net error-handling application-error

什么是处理错误的最佳方法,例如

从客户端检测到一个潜在危险的Request.Form值"

在ASP.NET中?

我想保持验证,因为我的表单没有正当理由允许HTML字符.但是,我不太确定如何以更友好的方式处理此错误.我尝试在一个处理它,Page_Error但据我所知,这发生在较低级别的部分,所以该Page_Error功能永远不会触发.

因此,我可能不得不求助于Application_Error在我的Global.asax文件中使用.如果这是处理该错误的唯一方法,有没有办法专门处理这个错误?我不想以同样的方式处理所有应用程序错误.

谢谢

Rub*_*ias 22

您有两种选择:

// Editing your global.asax.cs
public class Global : System.Web.HttpApplication
{
    protected void Application_Error(object sender, EventArgs e)
    {
        Exception lastError = Server.GetLastError();
        if (lastError is HttpRequestValidationException)
        {
            Response.Redirect("~/RequestValidationError.aspx");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

// Editing your CUser.aspx.cs
public partial class CUser : System.Web.UI.Page
{
    protected override void OnError(EventArgs e)
    {
        Response.Redirect("~/RequestValidationError.aspx");
        Context.ClearError();
    }
}
Run Code Online (Sandbox Code Playgroud)