c# - Process html files with HttpModule to catch 404 errors on IIS7 -


i having problem httpmodule handles exceptions. (cfr. previous post: custom httpmodule iis 7 integrated)

all works well, aspx pages.

the main reason wanted use httpmodule handle 404 exceptions occur when tries go html page doesn't exist. httpmodule works .aspx pages , isn't triggered when html file doesn't exist.

this configuration have set in web.conf file:

<system.webserver>   <modules>     <add name="aspexceptionhandler"           type="company.exceptions.aspexceptionhandler, company.exceptions"           precondition="managedhandler" />   </modules> </system.webserver> 

i have tried adding 'runallmanagedmodulesforallrequests="true"' module node, , 'precondition="managedhandler"' "add" node, didn't work.

i have set application pool on web application running "integrated" mode, because found lot on google.

is there way make httpmodule handle exceptions occur when visiting non existing html page?

thanks!

having done little research based on answer of alexander, implemented ihttphandler in aspexceptionhandler well.

my class looks like:

public class aspexceptionhandler : ihttpmodule, ihttphandler     {         public void dispose() { }          public void init(httpapplication context)         {             context.error += new eventhandler(errorhandler);         }          private void errorhandler(object sender, eventargs e)         {             httpapplication application = (httpapplication)sender;             try             {                 // gather information                 exception currentexception = application.server.getlasterror(); ;                 string errorpage = "http://companywebsite.be/error.aspx";                  httpexception httpexception = currentexception httpexception;                 if (httpexception == null || httpexception.gethttpcode() != 404)                 {                     application.server.transfer(errorpage, true);                 }                 //the error 404                 else                 {                     // continue                     application.server.clearerror();                      string shouldmail404 = true;                      //try , redirect proper page.                     string requestedfile = application.request.url.absolutepath.trim('/').split('/').last();                      // redirect if required                     string redirecturl = getredirecturl(requestedfile.trim('/'));                     if (!string.isnullorempty(redirecturl))                     {                         //redirect proper url                     }                     //if can't redirect properly, set statuscode 404.                     else                     {                         //report 404                     }                 }             }             catch (exception ex)             {                 exceptioncatcher.fillwebexception(httpcontext.current, ref ex);                 exceptioncatcher.catchexception(ex);             }         }          public bool isreusable         {             { return true; }         }          public void processrequest(httpcontext context)         {             if (!file.exists(context.request.physicalpath))              {                 throw new httpexception(404, string.format("the file {0} not exist", context.request.physicalpath));             }                     else             {                 context.response.transmitfile(context.request.physicalpath);             }         }     } 

in processrequest method (required ihttphandler) check if file exists. if not exist, throw httpexception catched httpmodule part of class.

the system.webserver node in web.config looks this:

<modules runallmanagedmodulesforallrequests="true">             <add name="aspexceptionhandler" type="company.exceptions.aspexceptionhandler, company.exceptions" precondition="managedhandler" />         </modules>         <handlers>             <add name="aspexceptionhandler" type="company.exceptions.aspexceptionhandler, company.exceptions" verb="*" path="*.html" />         </handlers> 

i found answer in in post: httphandler fire if file doesn't exist


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -