C# 不使用';我不在IIS7工作

C# 不使用';我不在IIS7工作,c#,.net,asp.net,iis-7,httpmodule,C#,.net,Asp.net,Iis 7,Httpmodule,我有一个HttpModule,它重定向ASP.NET WebForms应用程序中的某些URL:s。它与ASP.NET开发服务器一起在我的机器上工作。但当我用IIS7将它上传到我们的Win2k8服务器时,它似乎根本没有反应。我将放在system.webServer/modules部分,在inetmgr中,我可以在其他模块中看到该模块。在我上传代码之前和之后,网站的表现似乎都是一样的,这是不应该的 编辑的代码示例: public void Init(HttpApplication context)

我有一个HttpModule,它重定向ASP.NET WebForms应用程序中的某些URL:s。它与ASP.NET开发服务器一起在我的机器上工作。但当我用IIS7将它上传到我们的Win2k8服务器时,它似乎根本没有反应。我将
放在system.webServer/modules部分,在inetmgr中,我可以在其他模块中看到该模块。在我上传代码之前和之后,网站的表现似乎都是一样的,这是不应该的

编辑的代码示例:

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();
    }

也许我错过了什么。但是您是否在
system.webServer
部分定义了
HttpModule
?另外,您是否将
runAllManagedModulesForAllRequests
设置为true

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

显然,IIS7不接受使用HttpContext。错误如下:

context.Error += RedirectMethod;
context.AuthenticateRequest += RedirectMethod;
相反,我不得不这样做:

context.Error += RedirectMethod;
context.AuthenticateRequest += RedirectMethod;

这似乎有效。为什么,我不知道。我只是想在404被扔到用户面前之前,作为最后的手段重定向。是的,我把它放在那里了,但忘了在这里写下来。不管怎样,还是谢谢你。只需问一个显而易见的问题,并100%确定:你是把它放在System.Web还是System.webServer?System.webServer,我可以在IIS7的模块管理器中看到它,所以它就出现了。但它不起任何作用。