Home > httpmodule error > httpmodule error handler

Httpmodule Error Handler

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Custom error handling http module up vote 0 down vote favorite I am trying to intercept the errors occurred in all the sites on a web server in order to do other useful stuff, such as logging. As a starting point, I have Created http module which attaches an event handler to sites' error event. Created test MVC project with set of action methods throwing exceptions. Added the created http module class dll to GAC. Http module class library is implemented as: I have created a class library project with only one class extending IHttpModule. Please note that this is just a simplified version. using System; using System.Reflection; using System.Runtime.InteropServices; using System.Web; namespace MyWebModules { [ComVisibleAttribute(true)] public class MyErrorModule : IHttpModule { public void Dispose() { throw new NotImplementedException(); } public void Init(HttpApplication context) { context.Error += new EventHandler(OnError); } private void OnError(object sender, EventArgs e) { throw new Exception("Custom exception from MyErrorModule"); //TODO removed the above exception and add real life code here. } } } I added dll to GAC, by following the process below: Using csc.exe, I get ".netmodule" file from ".cs" code file ".snk" file is from the project. al.exe needs ".netmodule" and ".snk" to give strong name to the .dll With strong name, .dll is qualified to be added to GAC. Testing using MVC test project. Test site

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only http://stackoverflow.com/questions/33500288/custom-error-handling-http-module takes a minute: Sign up HttpModule for Error Handling and Missing Images up vote 5 down vote favorite 2 I have an HttpModule that I have put together from cobbling a couple of different sources online together into something that (mostly) works with both traditional ASP.NET applications, as well as ASP.NET MVC applications. The largest part of this comes from the http://stackoverflow.com/questions/1447350/httpmodule-for-error-handling-and-missing-images kigg project on CodePlex. My problem is in dealing with 404 errors due to a missing image. In the following code, I have had to explicitly look for an image being requested through the AcceptedTypes collection in the HttpContext's Request object. If I don't put in this check, even a missing image is causing a redirect to the 404 page defined in my section in the Web.config. The problem with this approach is that (beyond the fact it smells) is that this is just for images. I would basically have to do this with every single content type imaginable that I do not want this redirect behavior to happen on. Looking at the code below, can someone recommend some sort of refactoring that could allow for it to be more lenient with non-page requests? I would still want them in the IIS logs (so I would probably have to remove the ClearError() call), but I do not think that a broken image should impact the user experience to the point of redirecting them to the error page. The code foll

16 comments This post is the reason for me opening a blog. Recently I encountered a very simple problem that took me around 3 days to solve. I found many posts on this issue but nothing was what I needed. My task was http://blogs.microsoft.co.il/oshvartz/2008/05/17/aspnet-error-handling-using-httpmodule-full-and-partial-post-back-ajax-updatepanel/ quite simple I wanted a single point of logic to deal with the fatal errors: When http://forums.asp.net/t/1833446.aspx?handling+exceptions+inside+http+module an error occurs that is not a result of invalid input by the user - and we cannot recover from it. exception we didn't or couldn't catch (unhandled exceptions). I must say that most of my experience is with server side so my knowledge in ASP.NET is quite poor (Many thanks to Avi Pinto for all his help 😉 The error handling httpmodule error flow is: Log the Exception Open a generic error page with the error code number and more info. In ASP.NET there are two ways of doing this. The first is using the Global.asax file ( which I will not discuss here) and the other is using HttpModule ( a detailed explanation about HttpModules is beyond the scope of this article. You can read about it here). I choose the HttpModule because it's more reusable - if it needs httpmodule error handler to be altered you don't need to rebuild your whole web site, just add a reference DLL of the class that implements the interface IHttpModule and add to the web.config file one line: Implementing the IHttpModule: In the Init method enroll to the context error event. Implementing the error handler (my logic): just write to log, build query string for the error page and then Server.Transfer to the error page with the query string generated. Here I must add two notes about best practice of Exception handling in my opinion: Note 1: Some developers tend to use the exception technology for logic implementation (e.g. throw DataNotFoundException for empty data). I think it's bad practice, just return empty result. The Exception technology has very high overhead and wasn't design for logic implementation. Note 2: in your lower levels of logic ( e.g. code behind of a page ) don't catch Exception or WebException unless you have something to do with them: the only reason to catch general Excption is maybe to add information and then rethrow it. I tested the HttpModule just by removing the catch(Exception ex) lines from the solution I was working on. It appeared to be working 🙂 but trying it on another web page it crashed. I checked why and discovered th

ASP.NET Community Standup Forums Help Home/ASP.NET Forums/Advanced ASP.NET/HttpHandlers and HttpModules/handling exceptions inside http module handling exceptions inside http module [Answered]RSS 1 reply Last post Aug 13, 2012 10:35 AM by BrockAllen ‹ Previous Thread|Next Thread › Print Share Twitter Facebook Email Shortcuts Active Threads Unanswered Threads Unresolved Threads Support Options Advanced Search Reply sharepoint99 Member 9 Points 38 Posts handling exceptions inside http module Aug 13, 2012 08:58 AM|sharepoint99|LINK Hello, I have a handler which operates at the level context.AuthenticateRequest += new EventHandler(Application_AuthenticateRequest); I just needed some information as to what happens if an exception occurs in this event and how to handle this. And also if I want to access a configuration value stored in the site this module is throwing an error after an iisreset and working on the next page refresh.What I am thinking is that after an iisreset the worker process is not available at the time the module is looking to get some config value from the site (not the web.config.the site is a sharepoint site ) am I thinking the right way. Thanks Reply BrockAllen All-Star 20376 Points 6502 Posts ASPInsidersMVP Re: handling exceptions inside http module Aug 13, 2012 10:35 AM|BrockAllen|LINK You can register an event handler for unhandled exceptions in the app via: context.Error += YourErrorEventHandler; As for exceptions for this specific event... do you mean in your code? If so, then just put your code in a try/catch. Brock Allen | http://brockallen.com DevelopMentor | http://www.develop.com thinktecture | http://www.thinktecture.com/ ‹ Previous Thread|Next Thread › This site is managed for Microsoft b

 

Related content

httpmodule error handling

Httpmodule Error Handling p Latest Articles Latest Tips Tricks Top Articles Beginner Articles Technical Blogs Posting Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your relatedl Blog quick answersQ A Ask a Question about this article Ask a Question View Unanswered Questions View All Questions C questions Linux questions ASP NET questions SQL questions VB NET questions discussionsforums All Message Boards Application Lifecycle Running a Business Sales Marketing Collaboration Beta Testing Work Issues Design and Architecture ASP NET JavaScript C C MFC ATL WTL STL Managed C CLI C Free Tools Objective-C and Swift Database

httpmodule error

Httpmodule Error p Latest Articles Latest Tips Tricks Top Articles Beginner Articles Technical Blogs Posting Update Guidelines Article Help Forum Article Competition Submit relatedl an article or tip Post your Blog quick answersQ A Ask a Question about this article Ask a Question View Unanswered Questions View All Questions C questions Linux questions ASP NET questions SQL questions VB NET questions discussionsforums All Message Boards Application Lifecycle Running a Business Sales Marketing Collaboration Beta Testing Work Issues Design and Architecture ASP NET JavaScript C C MFC ATL WTL STL Managed C CLI C Free Tools Objective-C and Swift Database Hardware