asp.net網(wǎng)站首頁根據(jù)IP自動跳轉指定頁面的示例
對于大中型網(wǎng)站,為了增強用戶體驗,往往需要根據(jù)不同城市站點的用戶推送或展現(xiàn)相應個性化的內(nèi)容,如對于一些大型門戶網(wǎng)站的新聞會有城市站點的功能,如果沒有設置相應的城市站點,默認就是根據(jù)用戶訪問的IP地址的所在城市自動設置。本文主要通過自定義擴展IHttpModule接口,考慮到性能IP數(shù)據(jù)庫主要采用QQwry純真IP數(shù)據(jù)庫,主要實現(xiàn)根據(jù)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節(jié)點添加此組件的配置,訪問限制或允許參數(shù)可以在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="鎮(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>
相關文章
Grid或者DataTable中數(shù)據(jù)導出為Excel原來這么簡單
以前一直認為,將Grid 或者DataTable中的數(shù)據(jù)導出到Excel功能實現(xiàn)會非常復雜,可能會想用什么類庫什么的或者實在太難就用csv算了,沒想到真的很簡單,需要了解的朋友可以參考下2012-12-12
Windows虛擬主機與VPS如何實現(xiàn)301重定向(asp.net)
301重定向應該是研究SEO必須掌握的技術。如果你是剛接觸SEO的菜鳥,想了解什么是301重定向,請看《html實現(xiàn)301重定向的方法》一文,我在該篇隨筆中引用了Google網(wǎng)站站長工具對301重定向的解釋2011-12-12
Asp.net控制Tomcat啟動關閉的實現(xiàn)方法
近日有個項目客戶要求能自己配置相關權限。由于歷史原因這個項目采用的是公司以前的權限系統(tǒng)2012-01-01

