public class HttpModule : System.Web.IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(ReUrl_BeginRequest);
}
private void ReUrl_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string requestPath = context.Request.Path;
List<PageUrls> urlList = GetPageUrls();
if (urlList.Any())
{
string htmlPageName = Path.GetFileName(requestPath);
var thisModel = urlList.Find(d => d.HtmlUrl == htmlPageName);
if (thisModel != null)
{
string realPageUrl = requestPath.Replace(htmlPageName, thisModel.RealUrl);
context.RewritePath(realPageUrl);
}
}
}
private List<PageUrls> GetPageUrls()
{
List<PageUrls> list = new List<PageUrls>();
XMLHelper xmlHelper = new XMLHelper();
string xmlPath = "/Config/PageUrlsXML.xml";
var data = xmlHelper.GetDataSetByXml(xmlPath);
if (data.Tables.Count > 0)
{
list = ListHelper.DtConvertToModel<PageUrls>(data.Tables[0]).ToList();
}
return list;
}
}