处理错误的最佳方式是什么?
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();
- }
- }