asp.net网站首页根据IP自动跳转指定页面的示例

 更新时间:2014年02月08日 15:14:12   作者:  
本文介绍的程序主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能,需要的朋友可以参考下

对于大中型网站,为了增强用户体验,往往需要根据不同城市站点的用户推送或展现相应个性化的内容,如对于一些大型门户网站的新闻会有城市站点的功能,如果没有设置相应的城市站点,默认就是根据用户访问的IP地址的所在城市自动设置。本文主要通过自定义扩展IHttpModule接口,考虑到性能IP数据库主要采用QQwry纯真IP数据库,主要实现根据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(@"~\IpData\qqwry.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>

相关文章

  • Visual Studio 2017 (VS 2017)离线安装包制作方法

    Visual Studio 2017 (VS 2017)离线安装包制作方法

    这篇文章主要为大家详细介绍了Visual Studio 2017离线安装包的制作方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • .Net Core Api 使用版本控制详解

    .Net Core Api 使用版本控制详解

    Api的版本控制是Api开发中经常遇到的问题, 在大部分中大型项目都需要使用到Api的版本控制,这篇文章主要介绍了.Net Core Api 使用版本控制详解,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • asp.net HTML文件上传标签

    asp.net HTML文件上传标签

    需要在要目录下新建两个目录:upfile和upimg 添加一个FileUpload控件.一个Button.一个Image.一个Label
    2008-12-12
  • Asp.Net 程序错误Runtime Error原因与解决

    Asp.Net 程序错误Runtime Error原因与解决

    提示这个,不管怎么改配置文件的设置都不行,下面是修正方法,大家可以试试。
    2010-03-03
  • 详解ASP.NET Core 网站发布到Linux服务器

    详解ASP.NET Core 网站发布到Linux服务器

    本篇文章主要介绍了ASP.NET Core 网站发布到Linux服务器 。具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • asp.net利用ashx文件实现文件的上传功能

    asp.net利用ashx文件实现文件的上传功能

    这篇文章主要介绍了asp.net利用ashx文件实现文件的上传功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • asp.net实现Postgresql快速写入/读取大量数据实例

    asp.net实现Postgresql快速写入/读取大量数据实例

    本篇文章主要介绍了asp.net实现Postgresql快速写入/读取大量数据实例,具有一定的参考价值,有兴趣的可以了解一下
    2017-07-07
  • [Asp.Net Core]提高开发效率的方法

    [Asp.Net Core]提高开发效率的方法

    这篇文章主要介绍了[Asp.Net Core]提高开发效率的方法,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • ASP.Net 分页控件源码

    ASP.Net 分页控件源码

    由于.net自带的分页功能极其死板,自定义性不强不能满足需求 俺花了一个多星期的时间 写成的一个.net分页控件
    2007-03-03
  • ASP.net处理XML数据实例浅析

    ASP.net处理XML数据实例浅析

    这篇文章主要介绍了ASP.net处理XML数据实例浅析,分析了XML的原理与用法,并以实例形式讲述了asp.net处理XML数据的方法,需要的朋友可以参考下
    2014-10-10

最新评论