HttpModule that does not work in IIS7

I have an HttpModule that redirects certain URLs: s in an ASP.NET WebForms application. It works on my machine with ASP.NET Development Server. But when I upload it to our Win2k8 server using IIS7, it does not seem to respond at all. I put <add name="Test.Web" type="Test.Web.Core.HttpModules.RedirectOldUrls, Test.Web" /> in the system.webServer / modules section, and inetmgr I see the module among others. The website seems to behave the same way as before I download the code that it shouldn't.

Edited code example:

 public void Init(HttpApplication context) { context.Error += PageNotFoundHandler; } public static void PageNotFoundHandler(object sender, EventArgs evt) { Exception lastErrorFromServer = HttpContext.Current.Server.GetLastError(); if (lastErrorFromServer != null) { RedirectToNewUrlIfApplicable(); } } private static void RedirectToNewUrlIfApplicable() { string redirectUrl = GetRedirectUrl(); if (!string.IsNullOrEmpty(redirectUrl)) { HttpContext.Current.Response.Status = "301 Moved Permanently"; HttpContext.Current.Response.AddHeader("Location", redirectUrl); } } private static string GetRedirectUrl() { return RedirectableUrls.GetUrl(); } 
+4
source share
2 answers

Apparently, IIS7 did not accept the use of HttpContext.Error, for example:

 context.Error += RedirectMethod; 

Instead, I had to do like this:

 context.AuthenticateRequest += RedirectMethod; 

This seems to work. Why i do not know. I just wanted to redirect as a last resort before the 404 would be reset before the user.

0
source

Maybe I missed something. But did you define your HttpModule in the system.webServer section? And also, you set runAllManagedModulesForAllRequests to true?

 <modules runAllManagedModulesForAllRequests="true"> <add name="You Module Name" type="Your Module Type"/> </modules> 
+3
source

Source: https://habr.com/ru/post/1301363/


All Articles