最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

asp.net網(wǎng)站首頁(yè)根據(jù)IP自動(dòng)跳轉(zhuǎn)指定頁(yè)面的示例

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:39:12
文檔

asp.net網(wǎng)站首頁(yè)根據(jù)IP自動(dòng)跳轉(zhuǎn)指定頁(yè)面的示例

asp.net網(wǎng)站首頁(yè)根據(jù)IP自動(dòng)跳轉(zhuǎn)指定頁(yè)面的示例:對(duì)于大中型網(wǎng)站,為了增強(qiáng)用戶體驗(yàn),往往需要根據(jù)不同城市站點(diǎn)的用戶推送或展現(xiàn)相應(yīng)個(gè)性化的內(nèi)容,如對(duì)于一些大型門(mén)戶網(wǎng)站的新聞會(huì)有城市站點(diǎn)的功能,如果沒(méi)有設(shè)置相應(yīng)的城市站點(diǎn),默認(rèn)就是根據(jù)用戶訪問(wèn)的IP地址的所在城市自動(dòng)設(shè)置。本文主要通過(guò)自定義擴(kuò)展I
推薦度:
導(dǎo)讀asp.net網(wǎng)站首頁(yè)根據(jù)IP自動(dòng)跳轉(zhuǎn)指定頁(yè)面的示例:對(duì)于大中型網(wǎng)站,為了增強(qiáng)用戶體驗(yàn),往往需要根據(jù)不同城市站點(diǎn)的用戶推送或展現(xiàn)相應(yīng)個(gè)性化的內(nèi)容,如對(duì)于一些大型門(mén)戶網(wǎng)站的新聞會(huì)有城市站點(diǎn)的功能,如果沒(méi)有設(shè)置相應(yīng)的城市站點(diǎn),默認(rèn)就是根據(jù)用戶訪問(wèn)的IP地址的所在城市自動(dòng)設(shè)置。本文主要通過(guò)自定義擴(kuò)展I

對(duì)于大中型網(wǎng)站,為了增強(qiáng)用戶體驗(yàn),往往需要根據(jù)不同城市站點(diǎn)的用戶推送或展現(xiàn)相應(yīng)個(gè)性化的內(nèi)容,如對(duì)于一些大型門(mén)戶網(wǎng)站的新聞會(huì)有城市站點(diǎn)的功能,如果沒(méi)有設(shè)置相應(yīng)的城市站點(diǎn),默認(rèn)就是根據(jù)用戶訪問(wèn)的IP地址的所在城市自動(dòng)設(shè)置。本文主要通過(guò)自定義擴(kuò)展IHttpModule接口,考慮到性能IP數(shù)據(jù)庫(kù)主要采用QQwry純真IP數(shù)據(jù)庫(kù),主要實(shí)現(xiàn)根據(jù)IP地址或地址段或IP所在城市進(jìn)行自動(dòng)跳轉(zhuǎn)到指定頁(yè)面的功能(支持Nginx作為前端反向代理服務(wù)器),該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)//忽略本地計(jì)算機(jī)請(qǐng)求
                //    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);  //查詢一個(gè)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
            {

            }
        }
    }
}

在部署方面,非常簡(jiǎn)單主要利用IHttpModule接口并在Web.config中的HttpModule節(jié)點(diǎn)添加此組件的配置,訪問(wèn)限制或允許參數(shù)可以在NetOpen_SystemWebsiteSkip.cfg.xml進(jìn)行設(shè)置,以下為一個(gè)簡(jiǎn)單的配置示例:

代碼如下:
<?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="鎮(zhèn)江" 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>

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

asp.net網(wǎng)站首頁(yè)根據(jù)IP自動(dòng)跳轉(zhuǎn)指定頁(yè)面的示例

asp.net網(wǎng)站首頁(yè)根據(jù)IP自動(dòng)跳轉(zhuǎn)指定頁(yè)面的示例:對(duì)于大中型網(wǎng)站,為了增強(qiáng)用戶體驗(yàn),往往需要根據(jù)不同城市站點(diǎn)的用戶推送或展現(xiàn)相應(yīng)個(gè)性化的內(nèi)容,如對(duì)于一些大型門(mén)戶網(wǎng)站的新聞會(huì)有城市站點(diǎn)的功能,如果沒(méi)有設(shè)置相應(yīng)的城市站點(diǎn),默認(rèn)就是根據(jù)用戶訪問(wèn)的IP地址的所在城市自動(dòng)設(shè)置。本文主要通過(guò)自定義擴(kuò)展I
推薦度:
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專題
Top