解决方案 2
我找到了解决方案。使用HttpModule非常容易。请查看我的以下示例代码。
在全球web.config
中<configuration>
<system.web>
<httpModules>
<add name="RedirectModule" type="MySolution.RedirectModule, MySolution" />
</httpModules>
</system.web>
<!-- The following code will be used by IIS 7+ -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="RedirectModule" />
<add name="RedirectModule" type="MySolution.RedirectModule MySolution" />
</modules>
</system.webServer>
</configuration>
在MySolution / RedirectModule.cs
中using System;
using System.Web;
namespace MySolution
{
public class RedirectModule : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(HttpApplication_BeginRequest);
}
public void Dispose() {}
#endregion
void HttpApplication_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (*[logic for checking before redirect]*)
{
app.Response.Redirect(*[url]*);
}
}
}
}
有关详细信息,请查看Url Mapping源代码,如 UrlRewritingNet.UrlRewrite 一>
PS。 IHttpModule是非常强大的接口。它可以处理每种请求类型。所以,它可以帮助我彻底解决这个问题。
其他提示
你应该确保-existing-url在路由表中'找到'/'匹配',并且你要重定向到的url不是....(除非你重定向到Controller Action / Route )
不隶属于 StackOverflow