简体   繁体   English

处理“潜在危险的Request.Form值......”

[英]Handle “potentially dangerous Request.Form value…”

What's the best way to handle errors such as 什么是处理错误的最佳方法,例如

A potentially dangerous Request.Form value was detected from the client" 从客户端检测到一个潜在危险的Request.Form值“

in ASP.NET? 在ASP.NET中?

I'd like to keep the validation on, as my forms have no valid reasons to be allowing HTML characters. 我想保持验证,因为我的表单没有正当理由允许HTML字符。 However, I'm not quite sure how to handle this error in a more friendly manner. 但是,我不太确定如何以更友好的方式处理此错误。 I tried handling it in a Page_Error but, as far as I can tell, this occurs in a lower level section so the Page_Error function never fires. 我尝试在Page_Error处理它,但据我所知,这发生在较低级别的部分,因此Page_Error函数永远不会触发。

Therefore, I may have to resort to using Application_Error in my Global.asax file. 因此,我可能不得不求助于在Global.asax文件中使用Application_Error If this is the only way of handling that error, is there a way of specifically handling that one error? 如果这是处理该错误的唯一方法,有没有办法专门处理这个错误? I don't want to handle all application errors in the same manner. 我不想以同样的方式处理所有应用程序错误。

Thanks 谢谢

You have two options: 您有两种选择:

// 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");
        }
    }
}

Or 要么

// 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();
    }
}

You don't want to go adding unnecessary baggage to the Global.asax. 你不想在Global.asax上添加不必要的包袱。 If you're satisfied that this is caused by spurious data input, then deal with the input, no matter where it's coming from: 如果您确信这是由虚假数据输入引起的,那么无论它来自何处,都要处理输入:

http://codersbarn.com/post/2008/11/01/ASPNET-Data-Input-Validation.aspx http://codersbarn.com/post/2008/11/01/ASPNET-Data-Input-Validation.aspx

Concentrate on the cause of the error :-) 专注于错误的原因:-)

您可以在Application_Error中使用Server.GetLastError()来获取抛出的异常,检查异常,并根据需要进行响应(重定向到页面等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM