简体   繁体   English

一个在IIS7中不起作用的HttpModule

[英]A HttpModule that doesn't work in IIS7

I have a HttpModule that redirects certain URL:s in an ASP.NET WebForms application. 我有一个HttpModule重定向ASP.NET WebForms应用程序中的某些URL。 It works on my machine with the ASP.NET Development Server. 它可以在我的机器上使用ASP.NET Development Server。 But when I upload it to our Win2k8 server with IIS7, it doesn't seem to react at all. 但是当我用IIS7将它上传到我们的Win2k8服务器时,它似乎根本没有反应。 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. 我在system.webServer / modules部分放了<add name="Test.Web" type="Test.Web.Core.HttpModules.RedirectOldUrls, Test.Web" /> ,在inetmgr中我可以看到模块在其他的。 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();
    }

Maybe I'm missing something. 也许我错过了什么。 But did you define your HttpModule in the system.webServer section? 但是你在system.webServer部分定义了你的HttpModule吗? And also, did you set the runAllManagedModulesForAllRequests to true? 而且,你是否将runAllManagedModulesForAllRequests设置为true?

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

Apparently, the IIS7 did not accept using HttpContext.Error, like this: 显然,IIS7不接受使用HttpContext.Error,如下所示:

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. 我只想在将404转移到用户面前之前重定向作为最后的手段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的HttpHandler不适用于IIS7中的虚拟目录,但是使用cassini可以吗? - My HttpHandler doesn't work with a virtual directory in IIS7, but using cassini it does? Microsoft.Web.Administration(用于IIS7自动化)在VS 2010中不起作用 - Microsoft.Web.Administration (for IIS7 automation) doesn't work in VS 2010 使用HttpModule处理html文件以捕获IIS7上的404错误 - Process html files with HttpModule to catch 404 errors on IIS7 IIS7 HTTPModule无法重写MVC子应用程序的路径 - IIS7 HTTPModule not able to rewrite path to a MVC sub application 尽管有WWWfilter,但已部署IIS7的MVC仍未重定向 - IIS7 deployed MVC doesn't redirect despite WWWfilter HttpModule.Init - 在IIS7集成模式下安全地添加HttpApplication.BeginRequest处理程序 - HttpModule.Init - safely add HttpApplication.BeginRequest handler in IIS7 integrated mode 为什么BasicHttpBinding不需要主体名称就可以在IIS7上使用Kerberos Auth? - Why doesn't BasicHttpBinding need a Principal Name to use Kerberos Auth on IIS7? Process.Start权限 - 不会在iis7服务器上执行 - Process.Start permissions - doesn't get executed on iis7 server IIS7的自定义重定向不会传递If-Modified-Since标头。 错误? - IIS7's custom redirection doesn't pass If-Modified-Since header. Bug? Response.AppendToLog()在IIS7,Windows Server 2008中不附加任何内容 - Response.AppendToLog() doesn't append anything in IIS7, Windows Server 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM