使用母版页的自定义错误处理程序
我有一个像这样的自定义错误处理程序类:
namespace AccountCenterUserControls
{
public class EWHErrorModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.Error += new System.EventHandler(OnError);
}
public void OnError(object obj, EventArgs args)
{
Page myPage = (System.Web.UI.Page)HttpContext.Current.Handler;
ctx.Server.ClearError();
}
public void Dispose() { }
}
}
我已经在 web.config 中实例化了这个错误处理程序,如下所示:
<httpModules>
<!-- EWH Custom Error Handler -->
<add type="AccountCenterUserControls.EWHErrorModule" name="EWHErrorModule"/>
</httpModules>
它可以捕获错误。我的问题是,如何让我的错误页面从我的母版页中受益?我可以从我的页面访问各个母版页,但我不太确定当我处于如此深度时如何设置 contentplaceholder。
I have a custom error handler class like this:
namespace AccountCenterUserControls
{
public class EWHErrorModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.Error += new System.EventHandler(OnError);
}
public void OnError(object obj, EventArgs args)
{
Page myPage = (System.Web.UI.Page)HttpContext.Current.Handler;
ctx.Server.ClearError();
}
public void Dispose() { }
}
}
I've instantiated this error handler in my web.config like this:
<httpModules>
<!-- EWH Custom Error Handler -->
<add type="AccountCenterUserControls.EWHErrorModule" name="EWHErrorModule"/>
</httpModules>
It traps errors ok. My question is, how can I let my trapped error pages benefit from my masterpage? I can get to the individual masterpages from my page, but I'm not quite sure how I set the contentplaceholder when I am in this deep.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ASP.NET 中,应用程序错误事件可以在页面生命周期中的任何时刻引发,您可能正在执行错误处理程序,而修改页面是完全非法的。如果允许,您可以使用 FindControl 获得对要修改或替换的元素的引用。同样,如果时机合法,您可以修改控件的控件集合,或者从控件的控件集合中添加或删除控件,以实现最终所需的呈现。
我希望我对您的情况的假设是正确的如果是的话我确实想知道为什么您不简单地在 Global.asax/.cs 中实现 Application_Error ?这将自动连接到 ASP.NET 应用程序的 OnError 事件。在该处理程序中,您将拥有可用于请求、响应和会话的引用。
另外,您应该知道 HttpContext.Current.Handler 可以并且将会在生命周期的某些阶段返回 null。例如,在将请求传递给处理程序之前发生了错误,例如在验证请求期间。
In ASP.NET the app error event can be raised at any point during the page life-cycle you may be executing your error handler at a time it is entirely illegal to modify the Page. If it is allowable you can attain a reference to the element you wish to modify or replace using FindControl. Again, if the timing is legal you can modify the controls collection of the control or add or remove controls from the control's control collection to achive the eventual desired rendering.
I hope my assumptions are correct about your situation If they are I did wonder why you do not simply implement Application_Error in Global.asax/.cs? This would be automatically wired to the ASP.NET application's OnError event. Inside that handler you would have references available to Request, Response, and Session.
Also, you should be aware that HttpContext.Current.Handler can and will return null at certain stages of the life-cycle. For example an error has occurred before the request is handed off to the handler like during authenticate request.