Implementation of ihttphandler interface for URL rewriting

Source: Internet
Author: User
In the past, Ms urlrewriter was used for URL rewriting. Many shortcomings were found, and the custom functions were too weak. With the addition of rewrite rules, the web. in fact, URL rewriting is the implementation of the ihttphandler interface.
The entire process is divided into two steps:

1. Use an XML file to store rewrite rules. These rules are some simple regular expressions.
2. Implement the ihttphandler Interface
First, let's take a look at the XML file format:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Root> <RegEx> <! -- Rewrite the virtual address --> <B> <! [CDATA [xxx ,(? <ID> 00000-9000000000000.html $]> </B> <! -- Actual address --> <A> <! [CDATA [xxx. aspx? Id =$ {ID}]> </a> </RegEx> </root>

I believe everyone can understand the XML above.

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, used to store the public stru data read from the XML file CT 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 ippublic struct ipfilter {Public String _ IP; Public ipfilter (string IP) {_ IP = IP ;}} public class htmlhttphandler: ihttphandler // implement the ihttphandler interface {private list <regexinfo> _ regex_list = new list <regexinfo> (); Private list <ipfilte R> _ ip_filter = new list <ipfilter> (); Public htmlhttphandler () {dataset DS = new dataset (); // read the URL rewriting rule file, and DS is written to the instance in the regexinfo structure. 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 list of blocked IP addresses 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; // obtain ipforeach (ipfilter R in _ ip_filter) {If (_ IP = R. _ IP) {context. response. write ("sorry, your IP:" + _ IP + "is restricted! "); Context. response. end () ;}} string Path = context. request. path. tolower (); // obtain the currently accessed overwritten false urlforeach (regexinfo R in _ regex_list) Path = RegEx. replace (path, R. _ before, R. _ after); // match the actual urlcontext. server. execute (PATH);} // override the isreusable property. public bool isreusable {get {return true ;}}}

OK. The ihttphandler interface is implemented. You can rewrite the URL by configuring web. config.
In <system. Web> </system. Web> of Web. config, add

 
<Httphandlers> <add verb = "*" Path = "*. html" type = "htmlhttphandler"/>  

All files suffixed with .html are handed over to the htmlhttphandler class for processing.
Finally, configure IIS.
For simple conversions, you can add them to processrequest. for how to implement conversions, go to my blog and find them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.