views:

319

answers:

2

I have a HttpModule that redirects certain URL:s in an ASP.NET WebForms application. It works on my machine with the ASP.NET Development Server. But when I upload it to our Win2k8 server with IIS7, it doesn't seem to react at all. I've put the <add name="Test.Web" type="Test.Web.Core.HttpModules.RedirectOldUrls, Test.Web" /> in the system.webServer/modules section, and in inetmgr I can see the module amongst the other ones. The website seems to behave the same way before and after I upload the code, which it shouldn't.

An 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();
    }
+1  A: 

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

<modules runAllManagedModulesForAllRequests="true">
    <add name="You Module Name" type="Your Module Type"/>
</modules>
Mehdi Golchin
Yes I did put it there, but forgot to write it down here. Thanks anyway.
Microserf
Just to ask the obvious and be 100% sure:Did you put it in System.Web or System.webServer?
Marvin Smit
system.webServer, I can see it in the module manager in IIS7 so it shows up. But it doesn't do any work.
Microserf
A: 

Apparently, the IIS7 did not accept using HttpContext.Error, like this:

context.Error += RedirectMethod;

Instead, I had to do like this:

context.AuthenticateRequest += RedirectMethod;

Which seems to work. Why, I don't know. I only wanted to redirect as a last resort before a 404 is dumped in the face of the user.

Microserf