Interface
The previous use of the URL rewrite is used in the MS Urlrewriter, used later found a lot of deficiencies, the custom function is too weak, and with the increase in rewrite rules, Web.config may become larger, in fact, URL rewriting is to implement IHttpHandler interface.
The whole process is divided into two steps:
1. Use an XML file to store rewrite rules, where these rules are simple regular expressions
2. Implement IHttpHandler Interface
First look at the format of the XML file:
!--rewrite the following virtual address-->
! [Cdata[xxx, (? [0-9]+). html$]]>
!--actual Address-->
! [cdata[xxx.aspx?id=${id}]]>
I believe the XML above can be read by all.
Using System;
Using System.IO;
Using System.Data;
Using System.Configuration;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Text;
Using System.Text.RegularExpressions;
Using Microsoft.VisualBasic;
Regexinfo structure for storing data read from an XML file
public struct Regexinfo
{
public string _before;
public string _after;
Public Regexinfo (string before, string after)
{
_before = before. ToLower ();
_after = after. ToLower ();
}
}
IPFilter structure, used to store the blocked IP
public struct IPFilter
{
public string _ip;
Public ipfilter (String IP)
{
_IP = IP;
}
}
public class Htmlhttphandler:ihttphandler//Implementation IHttpHandler interface
{
Private List <RegexInfo> _regex_list = new list <RegexInfo> ();
Private List <ipFilter> _ip_filter = new list <ipFilter> ();
Public Htmlhttphandler ()
{
DataSet ds = new DataSet ();
Reads the URL rewrite rule file and writes to an instance of the REGEXINFO structure
Ds. READXML (System.Web.HttpContext.Current.Server.MapPath ("~/app_data/regexs.xml"));
foreach (DataRow R in DS. tables["Regex"]. Rows)
_regex_list. ADD (New Regexinfo (((string) r["B"]). Trim (), ((string) r["a"]). Trim ());
Ds. Reset ();
Read the Blocked IP list
Ds. READXML (System.Web.HttpContext.Current.Server.MapPath ("~/app_data/ipfilter.xml"));
foreach (DataRow R in DS. tables["Ipfilters"]. Rows)
_ip_filter. ADD (New IPFilter ((string) r["IP"));
}
public void ProcessRequest (HttpContext context)
{
String _ip = context. request.userhostaddress; Get IP
foreach (IPFilter R in _ip_filter)
{
if (_ip = = r._ip)
{
Context. Response.Write ("Sorry, your IP:" +_ip+ "has been limited!");
Context. Response.End ();
}
}
String path = context. Request.Path.ToLower (); Gets the overridden false URL of the current access
foreach (Regexinfo R in _regex_list)
Path = Regex.Replace (path, R._before, R._after); Match its real URL.
Context. Server.Execute (path);
}
Override the IsReusable property.
public bool IsReusable
{
get {return true;}
}
}
Ok,ihttphandler interface is implemented, below a little bit with web.config can be implemented URL rewrite
In the Web.config , add:
Indicates that the file with the suffix named. html is all handed to the Htmlhttphandler class to handle, and finally with IIS on the line.
As for the transformation, you can add to the ProcessRequest, as to how to implement the conversion to see the next page.