开发者

Custom Error Handler that uses a Master Page

开发者 https://www.devze.com 2023-02-14 13:13 出处:网络
I have a custom error handler class like this: namespace AccountCenterUserControls { public class EWHErrorModule : IHttpModule

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="AccountCenterUserCo开发者_如何学Pythonntrols.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消