From my last question,我发现Asp.net路由不能使用现有的URL进行路由以获取更多信息read this。通过使用Asp.net路由不可能路由现有的URL吗?
如何使用Asp.net路由将现有url重定向到另一个url?
提示!
我认为,这种像访问这样的行为会拒绝总是重定向到默认登录页面的行为。
感谢,
From my last question,我发现Asp.net路由不能使用现有的URL进行路由以获取更多信息read this。通过使用Asp.net路由不可能路由现有的URL吗?
如何使用Asp.net路由将现有url重定向到另一个url?
提示!
我认为,这种像访问这样的行为会拒绝总是重定向到默认登录页面的行为。
感谢,
我找到了解决方案。使用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映射源代码一样UrlRewritingNet.UrlRewrite。
PS。 IHttpModule是非常强大的界面。它可以处理每种请求类型。所以,它可以帮助我彻底解决这个问题。
你应该确保-existing-网址为“发现”在路由表/“匹配”,并指向的网址要重定向不....(除非你正在重定向到控制器动作/路由)
如果你有答案你为什么问这个问题? – Avitus 2009-06-17 11:12:08
我没有任何答案。你有什么主意吗? – 2009-06-17 11:29:20