如何在生产中捕获 HttpRequestValidationException

发布于 2024-10-18 00:32:49 字数 775 浏览 6 评论 0原文

我有这段代码来处理我的 global.asax.cs 文件中的 HttpRequestValidationException 。

protected void Application_Error(object sender, EventArgs e)
{
    var context = HttpContext.Current;
    var exception = context.Server.GetLastError();
    if (exception is HttpRequestValidationException)
    {
        Response.Clear();
        Response.StatusCode = 200;
        Response.Write(@"<html><head></head><body>hello</body></html>");
        Response.End();
        return;
    }
}

如果我调试我的网络应用程序,它会完美运行。但是,当我将其放在生产服务器上时,服务器会忽略它并生成“从客户端检测到潜在危险的 request.form 值” - 错误页面。 我不知道具体发生了什么... 如果有人知道问题是什么,或者我做错了什么......?

另外,我不想在 web.config 中将 validaterequest 设置为 false。

服务器使用IIS7.5,我使用的是asp.net 3.5。

谢谢, 布鲁诺

I have this piece of code to handle the HttpRequestValidationException in my global.asax.cs file.

protected void Application_Error(object sender, EventArgs e)
{
    var context = HttpContext.Current;
    var exception = context.Server.GetLastError();
    if (exception is HttpRequestValidationException)
    {
        Response.Clear();
        Response.StatusCode = 200;
        Response.Write(@"<html><head></head><body>hello</body></html>");
        Response.End();
        return;
    }
}

If I debug my webapplication, it works perfect. But when i put it on our production-server, the server ignores it and generate the "a potentially dangerous request.form value was detected from the client" - error page.
I don't know what happens exactly...
If anybody knows what the problem is, or what i do wrong..?

Also I don't want to set the validaterequest on false in the web.config.

The server uses IIS7.5, And I'm using asp.net 3.5.

Thanks,
Bruno

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本宫微胖 2024-10-25 00:32:50

好吧,我自己找到的。
我必须清除我最后的错误。

protected void Application_Error(object sender, EventArgs e)
{
    var context = HttpContext.Current;
    var exception = context.Server.GetLastError();
    if (exception is HttpRequestValidationException)
    {
        context.Server.ClearError();    // Here is the new line.
        Response.Clear();
        Response.StatusCode = 200;
        Response.Write(@"<html><head></head><body>hello</body></html>");
        Response.End();
        return;
    }
}

Ok, i found it my self.
I must clear my last error.

protected void Application_Error(object sender, EventArgs e)
{
    var context = HttpContext.Current;
    var exception = context.Server.GetLastError();
    if (exception is HttpRequestValidationException)
    {
        context.Server.ClearError();    // Here is the new line.
        Response.Clear();
        Response.StatusCode = 200;
        Response.Write(@"<html><head></head><body>hello</body></html>");
        Response.End();
        return;
    }
}
歌枕肩 2024-10-25 00:32:50

另一种仅适用于 MVC 的方法是使用自定义异常过滤器:

  • 创建一个自定义 FilterAttribute,
  • 从 FilterAttribute 内部实现 IExceptionFilter,您可以重定向到用于显示错误的控制器或视图。
  • 在 Global.asax 中注册过滤器或将其添加到您的控制器中

这样做的优点是您可以使用普通的 MVC 基础设施 (Razor) 来呈现错误视图。

public class HttpRequestValidationExceptionAttribute : FilterAttribute, IExceptionFilter {

    public void OnException(ExceptionContext filterContext) {
        if (!filterContext.ExceptionHandled && filterContext.Exception is HttpRequestValidationException) {
            filterContext.Result = new RedirectResult("~/HttpError/HttpRequestValidationError");
            filterContext.ExceptionHandled = true;
        }
    }
}

Another way that only works with MVC is using a custom Exception Filter:

  • Create a custom FilterAttribute that implements IExceptionFilter
  • from inside the FilterAttribute, you can redirect to the controller or view to be used to display the error.
  • register the filter in the Global.asax or attribute your controllers

This has the advantage that you can use the normal MVC infrastructure (Razor) to render the error view.

public class HttpRequestValidationExceptionAttribute : FilterAttribute, IExceptionFilter {

    public void OnException(ExceptionContext filterContext) {
        if (!filterContext.ExceptionHandled && filterContext.Exception is HttpRequestValidationException) {
            filterContext.Result = new RedirectResult("~/HttpError/HttpRequestValidationError");
            filterContext.ExceptionHandled = true;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文