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

前端之家收集整理的这篇文章主要介绍了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.如果这是处理该错误的唯一方法,是否有专门处理该错误方法?我不想以同样的方式处理所有的应用程序错误.

谢谢

解决方法

你有两个选择:
  1. // Editing your global.asax.cs
  2. public class Global : System.Web.HttpApplication
  3. {
  4. protected void Application_Error(object sender,EventArgs e)
  5. {
  6. Exception lastError = Server.GetLastError();
  7. if (lastError is HttpRequestValidationException)
  8. {
  9. Response.Redirect("~/RequestValidationError.aspx");
  10. }
  11. }
  12. }

要么

  1. // Editing your CUser.aspx.cs
  2. public partial class CUser : System.Web.UI.Page
  3. {
  4. protected override void OnError(EventArgs e)
  5. {
  6. Response.Redirect("~/RequestValidationError.aspx");
  7. Context.ClearError();
  8. }
  9. }

猜你在找的asp.Net相关文章