Call the default asp.net HttpHandler from a custom handler

后端 未结 3 1062

I\'m adding ASP.NET routing to an older webforms app. I\'m using a custom HttpHandler to process everything. In some situations I would like to map a particular pat

3条回答
  •  清酒与你
    2021-02-03 11:23

    Another option is to invert the logic by handling the cases in which you do NOT want to return the default response and remap the others to your own IHttpHandler. Whenever myCondition is false, the response will be the "default". The switch is implemented as an IHttpModule:

    public class SwitchModule: IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PostAuthenticateRequest += app_PostAuthenticateRequest;
        }
    
        void app_PostAuthenticateRequest(object sender, EventArgs e)
        {
            // Check for whatever condition you like           
            if (true)
                HttpContext.Current.RemapHandler(new CustomHandler());
    
        }
    
        public void Dispose()        
    }
    
    internal class CustomHandler: IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("hallo");
        }
    
        public bool IsReusable { get; }
    }
    

提交回复
热议问题