[英]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.