一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!

摘要:
对于大中型网站,为了增强用户体验,通常需要根据不同城市站点的用户推送或显示相应的个性化内容。例如,对于一些大型门户网站,新闻将具有城市站点的功能。如果未设置相应的城市站点,默认情况下将根据用户访问IP地址的城市自动设置这些站点。本文主要通过定制来扩展IHttpModule接口。考虑到性能IP数据库主要采用QQwry纯IP数据库,主要实现根据IP地址或地址段或IP城市自动跳转到指定页面的功能。WebsiteSkip组件的核心代码如下:?
一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!

对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有城市站点的功能,如果没有设置相应的城市站点,默认就是根据用户访问的IP地址的所在城市自动设置。本文主要通过自定义扩展IHttpModule接口,考虑到性能IP数据库主要采用QQwry纯真IP数据库(但此数据库并非是官方的,我之前与ip138网站对比过,IP地址信息的准确性大概在90%左右),主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能(支持Nginx作为前端反向代理服务器),该WebsiteSkip组件核心代码如下:

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
    {
        #region IHttpModule 成员
 
        public void Dispose()
        {
        }
 
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
 
 
        #endregion
 
 
        void context_BeginRequest(object sender, EventArgs e)
        {
            try
            {
 
                //if (HttpContext.Current.Request.IsLocal)//忽略本地计算机请求
                //    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)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"].ToString();
                }
                else if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                }
               
 
                QQWryLocator qqWry = new QQWryLocator(HttpContext.Current.Server.MapPath(@"~IpDataqqwry.dat"));
 
                IPLocation ipaddress = qqWry.Query(ip);  //查询一个IP地址
 
                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.GetConfig().ExcludeUserAgents;
 
                if(Em.ExcludeUserAgentList.Count > 0)
                {
                    foreach (ExcludeUserAgent ua in Em.ExcludeUserAgentList)
                    {
                        if (HttpContext.Current.Request.UserAgent.Contains(ua.keyword))
                        {
                            return;
                        }
                    }
                }
 
                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)
                            {
                                if (sk.UrlFrom.Contains(ls_url) && !ls_url.Contains(sk.OutKeyWord))
                                {
                                    if (sk.UrlTo.Length > 0)
                                    {
                                        HttpContext.Current.Response.Redirect(sk.UrlTo, true);
                                    }
                                    break;
                                }
                                
                            }
 
                            break;
                        }
                    }
                }
 
                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.Current.Response.Redirect(ls_urlto, true);
                            }
              
                        }
 
                    
                    }
                }
              
                 
            }
            catch
            {
 
            }
        }
    }
}

   在部署方面,非常简单主要利用IHttpModule接口并在Web.config中的HttpModule节点添加此组件的配置,访问限制或允许参数可以在NetOpen_SystemWebsiteSkip.cfg.xml进行设置,以下为一个简单的配置示例:

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<NetOpen_System>
  <WebsiteSkip>
    <SkipedUrl>
    <add ipcity="温州" 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="镇江" 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>
     <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">
    </ExcludeUserAgent>
  </WebsiteSkip>
  </WebsiteSkip>
</NetOpen_System>
复制代码

      该组件源代码下载地址:https://websiteskip.codeplex.com/,欢迎访问下载!虽然该组件实现并不复杂,原理也很简单,但较为实用,后续将增加根据IP138的网站进行实时查询,这样IP地址信息将更为精确,但对性能可能会有一些影响。

本博客为软件人生原创,欢迎转载,转载请标明出处:http://www.cnblogs.com/nbpowerboy/p/3164287.html 。演绎或用于商业目的,但是必须保留本文的署名软件人生(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。SharePoint商业智能技术QQ群:140668362,.Net技术交流QQ群:195516928,欢迎各位加入交流

 
 
分类: ASP.NETSharepoint

免责声明:文章转载自《一种基于自定义代码的asp.net网站首页根据IP自动跳转指定页面的方法!》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇iOS 控制器View的生命周期及相关函数DB2函数大全下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

在线音乐网站【05】测试

  需求、功能结构、数据库设计、功能实现都总结完。   就等于是盖一座房子,风水看了、地基打了、房子取向也决定了、墙也砌起来了、油漆也刷上了。   嗯,好的。现在是不是该住进房子享受一下了,如果想看看我的房子是什么样子的,可以看看我前面几篇博文:   1.在线音乐网站(1)需求和功能结构   2.在线音乐网站(2)数据库和开发环境   3.在线音乐网站(3...

Sencha Touch开发实例:有动画效果iPad的网站首页

在51CTO的专题“Sencha Touch入门教程”中我们已对如何使用Sencha touch进行开发有了初步的认识。在本系列教程中,将学习如何使用Sencha Touch,开发一个适合在iPad上运行的网页应用,并详细讲解其中的技巧。本文的阅读对象为对Sencha Touch有一定基础的读者,如果读者不熟悉相关内容,请先查看51CTO的专题。 构图设计...