locked
HttpModule for error handling fails in IIS7 RRS feed

  • Question

  • User-1665555424 posted

    This is posted here because it might be module related.
    MVC 3+, Win7 (development)  (IIS7 production)

    Using VS2010 Local server my HttpModule for exception handling works fine.  When going to IIS I get the yellow screen of death.

    The example below is for MVC, but an extremely similar Module which worked for years for Asp.net applications is now failing the same way.  It differed by instead of returning a view, returned an embedded aspx page.

    So I tried messing with the web.config by using httpErrors and I tried messing with the response by adding: TrySkipIisCustomErrors

    But nothing is working.

    I have an HttpModule with

    ExceptionHandlerHelper : IHttpModule
        application.Error += new EventHandler(LogUheToUI); //regular UHE
    


    Web.Config
        <httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough" >     
        </httpErrors>
     <system.webServer>     
    <
    httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough" >         
    <
    modules runAllManagedModulesForAllRequests="true">       <add name="ExceptionHandlerHelper" type="MvcUtil.ExceptionHandler.ExceptionHandlerHelper"/>     </modules>
    
    
    <system.web>
    
        <httpModules>
          <add name="ExceptionHandlerHelper" type="MvcUtil.ExceptionHandler.ExceptionHandlerHelper"/>
        </httpModules>



     private static void LogUheToUI()
            {
                HttpContext ctx = HttpContext.Current;
                HttpRequestWrapper req = new HttpRequestWrapper(ctx.Request);
                Exception ex = ctx.Server.GetLastError();
                ctx.Response.Clear();
                
     
                if (ctx.CurrentHandler != null)  // i.e. we are in the MVC handler
                {
                    RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
     
                    ctrler c = new ctrler();
                    ControllerContext cc = new ControllerContext(rc, c);
     
                    ViewResult viewResult = new ViewResult { ViewName = "Error" };  //This needs to be a view in the real app which will load the partial view from the dll.
                    viewResult.ViewBag.Title = FormatDisplayString("(ApplicationName) problem");
                    viewResult.ExecuteResult(cc);
     
                    // If ajax request we need to tell it that failure occured.  
                    // So we put in a 400 Status Code.  Otherwise it would get this error page back as a success result.
                    // This error page will be returned in the Error response. 
                    if (req.IsAjaxRequest())
                        rc.HttpContext.Response.StatusCode = 400;
     
                   ctx.Response.TrySkipIisCustomErrors = true;
                    ctx.Server.ClearError();
                }
                else if (ex is HttpRequestValidationException)  //this occurs before MVC handler
                {
                    ctx.Response.Clear();
                    
                    string resMsg = "<html><body><span style=\"font-size: 12pt; color: red\">"
                        + "Your input contains text that is not allowed.<br />"
                        + "Text provided cannot contain character combinations such as :"
                        + "<ul><li>&lt;janedoe@lanl.gov&gt;</li><li>&amp;#949;</li><li>&lt;Text</li></ul>"
                        + "The application treats this as an attempt to enter a potentially malicious programming string that could be executed when a person browses a webpage. Please review your input and remove any entries that resemble the above."
                        + "<br/>";
     
                    if (req.IsAjaxRequest()) //if ajax request we have to set the status code to an error code so that ajax does not view this response as success.  On error a modal dialog will be shown with this response.
                        ctx.Response.StatusCode = 400;
                    else //if not ajax provide a javascript back button
                        resMsg = resMsg + "<br/><br/><input id=\"btnBack\" type=\"button\" value=\"Back\" onclick=\"history.back()\"/>";
     
                    resMsg = resMsg + "<br/></span></body></html>";
                        
                    ctx.Response.Write(resMsg);
                    ctx.Response.TrySkipIisCustomErrors = true;
                    ctx.Response.Flush();
                    ctx.Server.ClearError();
                    ctx.Response.End();
                }
     
            }
    
    Monday, May 21, 2012 6:34 PM

All replies

  • User-661350001 posted

    may be this if and else if conditions are failing

    try else condition and return something similar and check if that works

    Wednesday, May 23, 2012 6:33 AM
  • User-1665555424 posted

    Added an else just to write to response; No Change.  However, it still works when local on Win 7 vs web server or win7 webserver but not on iis7.

    I think it has something to do with:

                    ctrler c = new ctrler();
                    ControllerContext cc = new ControllerContext(rc, c);
     
                    ViewResult viewResult = new ViewResult { ViewName = "Error" };  //This needs to be a view in the real app which will load the partial view from the dll.

     Maybe the VirtualPathProviders work differently in iis7??? 
    Or maybe the compilation is different and the embedded view can't be found??
    I tried to post this question in another area but MS deleted it.          
    Wednesday, May 23, 2012 9:28 AM