Process html files with HttpModule to catch 404 errors on IIS7

I am having another problem with my HttpModule which handles exceptions. (cfr. my previous post: Custom HttpModule for IIS 7 for integrated )

Everything works fine, but only for aspx pages.

The main reason we wanted to use this HttpModule is to handle 404 exceptions that occur when someone tries to go to an html page that does not exist. But my HttpModule only works for .aspx pages and does not start when the html file does not exist.

This is the configuration I installed in the web.conf file:

<system.webServer> <modules> <add name="AspExceptionHandler" type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" preCondition="managedHandler" /> </modules> </system.webServer> 

I also tried adding 'runAllManagedModulesForAllRequests = "true" to the node module and' preCondition = "managedHandler" 'in the "add" node, but that didn't work either.

I installed the application pool my web application runs on in Integrated mode because I found it on Google.

Is there any other way to make my HttpModule handle exceptions that occur when visiting a non-existent html page?

Thanks!

+4
source share
2 answers

After doing a little research based on Alexander's answer, I also implemented IHttpHandler in my AspExceptionHandler.

Now my class is as follows:

 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 as HttpException; if (httpException == null || httpException.GetHttpCode() != 404) { application.Server.Transfer(errorPage, true); } //The error is a 404 else { // Continue application.Server.ClearError(); String shouldMail404 = true; //Try and redirect to the proper page. String requestedFile = application.Request.Url.AbsolutePath.Trim('/').Split('/').Last(); // Redirect if required String redirectURL = getRedirectURL(requestedFile.Trim('/')); if (!String.IsNullOrEmpty(redirectURL)) { //Redirect to the proper URL } //If we can't redirect properly, we set the statusCode to 404. else { //Report the 404 } } } catch (Exception ex) { ExceptionCatcher.FillWebException(HttpContext.Current, ref ex); ExceptionCatcher.CatchException(ex); } } public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { if (!File.Exists(context.Request.PhysicalPath)) { throw new HttpException(404, String.Format("The file {0} does not exist", context.Request.PhysicalPath)); } else { context.Response.TransmitFile(context.Request.PhysicalPath); } } } 

In the ProcessRequest method (IHttpHandler required) I check if the file exists. If this does not exist, I will throw an HttpException, which is caught by the HttpModule part of my class.

The system.webServer node in my web.config now looks like 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 the answer in this post: HttpHandler runs only if the file does not exist

+3
source

you can try this

 <httpHandlers> <add type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" verb="*" path="*"/> .... </httpHandlers> 

this will help you catch the whole request, but if the request is made for an existing page, you need to pass it to the standard handlers

+1
source

All Articles