首页 > 网站 > 建站经验 > 正文

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

2019-11-02 15:53:44
字体:
来源:转载
供稿:网友

 本文介绍的程序主要实现根据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(@"~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.OutKey
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表