【发布时间】:2010-02-15 17:10:29
【问题描述】:
我有一个 HttpModule 可以重定向 ASP.NET WebForms 应用程序中的某些 URL:s。它可以在我的机器上使用 ASP.NET 开发服务器。但是当我用 IIS7 将它上传到我们的 Win2k8 服务器时,它似乎根本没有反应。我已将<add name="Test.Web" type="Test.Web.Core.HttpModules.RedirectOldUrls, Test.Web" /> 放在 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();
}
【问题讨论】:
标签: c# .net asp.net iis-7 httpmodule