Url rewrite using IHttpModule
UrlRewriter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// <summary>
/// UrlRewriter 的摘要描述
/// </summary>
namespace idv.modules
{
public class UrlRewriter : IHttpModule
{
#region IHttpModule 成員
public void Dispose()
{
//throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context = application.Context;
if (context.Request.Path.EndsWith("TranslatedPage.html", StringComparison.CurrentCultureIgnoreCase))
{
string currentUrl = context.Request.Path;
// rewrite url [http://www.domain.com/actionValue/TranslatedPage.html]
// to [http://www.domain.com/TranslatedPage.aspx?action=actionValue]
string action = Regex.Match(currentUrl, "/([^/]+)/TranslatedPage.html", RegexOptions.IgnoreCase).Groups[1].Value;
context.RewritePath("~/TranslatedPage.aspx?action=" + action);
}
}
#endregion
}
}
Web.config
<httpModules>
<add name="UrlRewriter" type="idv.modules.UrlRewriter"/>
</httpModules>