asp.net – 处理“潜在的危险Request.Form值…”

处理错误的最佳方式是什么?

A potentially dangerous Request.Form value was detected from the client”

在ASP.NET中?

我想保持验证,因为我的表单没有正当理由允许HTML字符.但是,我不太清楚如何以更友好的方式处理这个错误.我尝试在一个Page_Error中处理它,但是据我所知,这发生在较低级别的部分,所以Page_Error函数从不触发.

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

谢谢

解决方法

你有两个选择:
// 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");
        }
    }
}

要么

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....