//The namespace used
using System;
using ;
using ;
using ;
using ;
using ;
using ;
//Note the namespace
namespace
{
//Inherited from IHttpModule interface
public class HttpModule :
{
/// <summary>
/// Init method to implement interface
/// </summary>
/// <param name="context"></param>
public void Init(HttpApplication context)
{
// Create a delegation to let it execute the following ReUrl_BeginRequest event
+= new EventHandler(ReUrl_BeginRequest);
}
/// <summary>
/// Implement the Dispose method of the interface
/// </summary>
public void Dispose()
{
}
private void ReUrl_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication) sender).Context;
string requestPath = ();
//SiteUrls is the following class, here you can refactor it
foreach ( url in ().Urls)
{
//Whether is the matching option found
if ((requestPath, , | ))
{
//Start replace it with the URL that our program can understand
string newUrl = ((("/")),
, , | );
//You can output it here.
//( + "<br>" + newUrl+"<br>");
//Start replace the URL that users can remember with URLs that can be understood by the program
(, , newUrl);
}
}
}
}
public class SiteUrls
{
//Defining it as volatile type is mainly for the convenience of multi-thread access. It has no practical significance in this example program, and it will be useful if the project is big.
private static volatile SiteUrls instance = null;
string UrlsFile = ("/config/");
//Define two properties
private _Urls;
public Urls
{
get { return _Urls; }
set { _Urls = value; }
}
//This is a key planting pair. It is not very deep at all.
private _Paths;
public Paths
{
get { return _Paths; }
set { _Paths = value; }
}
//Constructor
private SiteUrls()
{
Urls = new ();
Paths = new ();
//Read the CONFIG file in XML number
XmlDocument urlconfig = new XmlDocument();
(UrlsFile);
XmlNode root = ("urls");
foreach (XmlNode n in )
{
//If it is not a comment
if ( != && () == "rewrite")
{
XmlAttribute name = ["name"];
XmlAttribute path = ["path"];
XmlAttribute page = ["page"];
XmlAttribute querystring = ["querystring"];
XmlAttribute pattern = ["pattern"];
if (name != null && path != null && page != null
&& querystring != null && pattern != null)
{
(, );
//The url entity class is pressed into
(new URLRewrite(, , ("^", "&"),
("^", "&")));
}
}
}
}
//Instantiate with methods
public static SiteUrls GetSiteUrls()
{
if (instance == null)
{
instance = new SiteUrls();
}
return instance;
}
#region url entity class
public class URLRewrite
{
#region Member variables
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Pattern;
public string Pattern
{
get { return _Pattern; }
set { _Pattern = value; }
}
private string _Page;
public string Page
{
get { return _Page; }
set { _Page = value; }
}
private string _QueryString;
public string QueryString
{
get { return _QueryString; }
set { _QueryString = value; }
}
#endregion
#region constructor
public URLRewrite(string name, string pattern, string page, string querystring)
{
_Name = name;
_Pattern = pattern;
_Page = page;
_QueryString = querystring;
}
#endregion
}
#endregion
}
}