如何在生产中捕获 HttpRequestValidationException
我有这段代码来处理我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我自己找到的。
我必须清除我最后的错误。
Ok, i found it my self.
I must clear my last error.
另一种仅适用于 MVC 的方法是使用自定义异常过滤器:
这样做的优点是您可以使用普通的 MVC 基础设施 (Razor) 来呈现错误视图。
Another way that only works with MVC is using a custom Exception Filter:
This has the advantage that you can use the normal MVC infrastructure (Razor) to render the error view.