asp.net site home page based on IP automatic jump to specify the sample pages

Source: Internet
Author: User
Tags foreach httpcontext servervariables trim

  The program introduced in this article mainly realizes the function of automatically jumping to the specified page according to the IP address or address segment or the city where the IP is located, and the friends who need it can refer to the following

For large and medium Web sites, in order to enhance the user experience, often need to be based on the different city site users to push or display the corresponding personalized content, such as for some large portal news will have the function of the city site, if not set the corresponding city site, the default is based on the user access to the city of the IP address automatically set This article mainly through the custom extension IHttpModule interface, considers the performance IP database mainly uses Qqwry pure IP database, mainly realizes the function which automatically jumps to the specified page according to the IP address or the address segment or the IP host city (supports Nginx as the front-end reverse proxy server), The core code for the Websiteskip component is as follows:   code is as follows: using System; Using System.Collections.Generic; Using System.Text; Using System.Web; Using System.Xml; Using System.IO; Using System.Net; Using System.Text.RegularExpressions; Using NetOpen_System.Component.QQWry;   namespace Netopen_system.component {    public sealed class Websiteskiphttpmodule:ihttpmodule   & nbsp {        #region IHttpModule member           public void Dispose ()   &NBSP ;     {       ,           public void Init (HttpApplication context)         {            context. BeginRequest + = new EventHandler (context_beginrequest);       &nbsp {            #endregion             void context_beginrequest (object sender, EventArgs e)         {            try     &NB Sp       {                 //if (HttpContext.Current.Request.Is Local computer request                //   return;                  //string IP = HttpContext.Current.Request.UserHostAddress;                //string IP = httpcontext.current.request.servervariables["REMOTE _addr "]. ToString ();                 String IP = string. Empty;                 if (httpcontext.current.request.servervariables["http_x_real_ip "]!= null)               &nbsp {                    IP = httpcontext.current.request.servervariables[" Http_x_real_ip "]. ToString ();                                 ELSE if (httpcontext.current.request.servervariables["http_x_forwarded_for"]!= null)                 {                    IP = HttpContext.Current.Requ Est. servervariables["Http_x_forwarded_for"]. ToString ();                                 ELSE if (httpcontext.current.request.servervariables["Http_via"]!= null)                 {                    IP = HttpContext.Current.Request.ServerVari ables["Http_x_forwarded_for"]. ToString ();         &nbsp      }                 else           &NB Sp     {                    IP = HttpContext.Current.Request.Ser vervariables["REMOTE_ADDR"]. ToString ();                                   & nbsp Qqwrylocator Qqwry = new Qqwrylocator (HttpContext.Current.Server.MapPath (@ "~ipdataqqwry.dat"));                   iplocation ipaddress = Qqwry.query (IP);  //query an IP address                   String ls_city = IPAddress. Country;                 String ls_urlfrom = string. Empty;                 String ls_urlto = string. Empty;                 string ls_url = httpcontext.cUrrent. Request.Url.AbsoluteUri;                 String Ls_useragentkeyword = string. Empty;                   Excludeuseragentmatchengine Em = WEBSITESKIPCONFIGURATION.G Etconfig (). excludeuseragents;                   if (Em.ExcludeUserAgentList.Count > 0)     &NBS P           {                    foreach (Exclud Euseragent UA in Em.excludeuseragentlist)                     {  & nbsp                     if (HttpContext.Current.Request.UserAgent.Contains ( Ua.keyword)                         {      &N Bsp                     return;     &NBsp                   {                & nbsp                   {                  Urlmatchengine PU = websiteskipconfiguration.getconfig (). Skipedurls;                   if (PU. Urllist.count > 0)                 {                    foreach (Skipedurl sk in Pu. urllist)                     {                          if (ls_city. Contains (SK. IpCity)                         {                            if (SK. Urlfrom.length > 0) &nbsp                                   nbsp                       if (SK. Urlfrom.contains (Ls_url) &&!ls_url. Contains (SK. Outkeyword)                               &NBS P {                                  & nbsp if (SK. Urlto.length > 0)                             &NB Sp       {                            & nbsp           HttpContext.Current.Response.Redirect (SK. Urlto, True);                                   &NBSp }                                   & nbsp Break                                }   & nbsp                          }         & nbsp                     break;                                   & nbsp        }                                   if (websiteskipconfiguration.getconfig). Ipchecks.getipin (IP)                 {                    Ls_urlfrom = Websiteskipconfiguration.getconfig (). Ipchecks. Urlfrom.trim ();                     Ls_urlto = Websiteskipconfiguration.getconfig (). IpChecks.UrlTo.Trim ();                     if (ls_urlfrom. Length > 0)                     {                          if (ls_urlfrom. Contains (Ls_url) &&!ls_url. Contains (Websiteskipconfiguration.getconfig (). Ipchecks.outkeyword)                         {                            if (Ls_urlto. Length > 0)                             {                                Httpcontext.curre Nt. Response.redirecT (Ls_urlto, true);                            }       & nbsp                   {                & nbsp                                  &N Bsp      }                             & nbsp               catch             {             }         {   }}     in the deployment aspect, very simple mainly utilizes the IHttpModule interface and in the W HttpModule nodes in Eb.config Add configuration for this component, access restrictions, or allow parameters to be set in NetOpen_SystemWebsiteSkip.cfg.xml, as follows a simple configuration example:     Copy code code as follows: <?xml version= "1.0" encoding= "Utf-8"?> <NetOpen_System>   <WebsiteSkip>   &NBsp <SkipedUrl>     <add ipcity= "Wenzhou" urlfrom= "http://examplesite.com/Default.aspx,http:// examplesite.com/,http://examplesite.cn/, http://www.examplesite.cn "urlto=" http://wz.mainwebsite.pcom/index.aspx "outkeyword=" math "/>     <add ipcity=" Zhenjiang "urlfrom=" http://examplesite.com/Default.aspx,http:// examplesite.com/,http://examplesite.cn/, http://www.examplesite.cn "urlto=" http://jszj.mainwebsite.com/ Index.aspx "outkeyword=" math "/>     </SkipedUrl>     <SkipedIP>      & Lt;add ip1= "220.186.0.0" ip2= 220.186.255.255 "urlfrom=" http://examplesite.com/Default.aspx,http:// examplesite.com/,http://examplesite.cn/, http://www.examplesite.cn "urlto=" http://wz.mainwebsite.com/index.aspx " outkeyword= "math"/>     </SkipedIP>     <ExcludeUserAgent>      < Add keyword= "Baiduspider" >      <add keyword= "Sosospider" >      <add keyword= "Sogou web Spider" >      <add keyword= "Sogou inst spider" >      <add keyword = "Sogou-test-spider" >      <add keyword= "Sogou Orion Spider" >      <add keyword= "Gigabot" >      <add keyword= "0JJJSpider" >      <add keyword= "Sogou Pic Spider ">      <add keyword=" Googlebot ">      <add keyword=" yeti/1.0 "> & nbsp   </ExcludeUserAgent>   </WebsiteSkip>   </WebsiteSkip> </NetOpen_System>  
Related Article

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.