📄 basepage.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using Kuqu.Component;
using System.Collections.Generic;
/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{
protected bool enableGenerateHTML = false;
protected bool IsPublish = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["IsPublish"]);
protected string htmlFileName = string.Empty;
public BasePage()
{
if (IsPublish)
{
string path = HttpContext.Current.Request.PhysicalPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath, string.Empty);
string key = string.Format("Website_SiteFiles_{0}", path.ToLower());
if (HttpContext.Current.Cache[key] != null)
{
this.enableGenerateHTML = Convert.ToBoolean(HttpContext.Current.Cache[key]);
}
else
{
string sql = string.Format("select EnableGenerateHTML from dbo.Website_SiteFiles where [Path] = N'{0}'", path);
object returnValue = Kuqu.Component.SqlHelper.ExecuteScalar(Kuqu.Component.DataHelper.ConnectionString, CommandType.Text, sql);
try
{
this.enableGenerateHTML = Convert.ToBoolean(returnValue);
}
catch
{
this.enableGenerateHTML = false;
}
HttpContext.Current.Cache.Insert(key, this.enableGenerateHTML, null, DateTime.Now.AddYears(1), TimeSpan.Zero);
}
htmlFileName = FileHelper.GetHtmlFileName(HttpContext.Current.Request.Url);
}
}
protected override void OnPreInit(EventArgs e)
{
if (RequestHelper.RequestQueryFieldAsInt("ISAJAXREQUEST") == 1)
{
ExecuteAjaxRequest(RequestHelper.RequestQueryFieldAsString("AJAXREQUESTFUNNAME"), HttpContext.Current);
}
if (!Page.IsPostBack && this.enableGenerateHTML == true && IsPublish == true
&& File.Exists(Request.MapPath(htmlFileName)))
{
Server.Transfer(htmlFileName);
}
base.OnPreInit(e);
}
protected override void Render(HtmlTextWriter writer)
{
string body = string.Empty;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
if (!IsPublish)//非发布状态时,显示执行时间
{
string time = (new TimeSpan(PageExcuteTime.GetTickCount() - (long)Context.Items["PageExcuteTime"]).TotalMilliseconds).ToString(); ;
string script = string.Format(@"<script>document.title+= '----执行时间:{0}毫秒';</script>", time);
base.ClientScript.RegisterClientScriptBlock(this.GetType(), "reter", script);
}
else//发布状态时,加入Google Analyse代码
{
//************插入流量监控************
string scriptGA = "<script type=\"text/javascript\">var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));</script><script type=\"text/javascript\">var pageTracker = _gat._getTracker(\"UA-4148157-1\");pageTracker._initData();pageTracker._trackPageview();</script>";
base.ClientScript.RegisterClientScriptBlock(this.GetType(), "GoogleAnalyse", scriptGA);
}
// 访问日志
Supervision();
base.Render(htmlTextWriter);
body = stringWriter.ToString();
stringWriter.Close();
htmlTextWriter.Close();
//SEO之标题META 开始 从数据库计Content,从页面会析keywords
if (!string.IsNullOrEmpty(StringHelper.Match(HttpContext.Current.Request.Url.AbsolutePath, @"detail\.aspx").Value) || !string.IsNullOrEmpty(StringHelper.Match(HttpContext.Current.Request.Url.AbsolutePath, @"default\.aspx").Value))
{
string url = HttpContext.Current.Request.Url.PathAndQuery;
string file = HttpContext.Current.Request.Url.AbsolutePath;
string pageKeywords = Kuqu.Application.Website_SEO.GetKeywords(url);
string fileKeywords = Kuqu.Application.Website_SiteFiles.GetKeywords(file);
if (string.IsNullOrEmpty(pageKeywords))
{
pageKeywords = Kuqu.Application.Website_HightFrequency.GetTags(body);
if (!string.IsNullOrEmpty(pageKeywords))
Kuqu.Application.Website_SEO.SaveKeywords(url, pageKeywords);
}
string keywords = string.Format("{0},{1}", pageKeywords, fileKeywords);
//this.keywords = keywords;
//this.description = keywords;
string str = string.Format("<head><meta name=\"description\" content=\"{0}\" /><meta name=\"keywords\" content=\"{0}\" />", keywords);
body = StringHelper.Replace(body, "<head>", str);
}
//SEO之标题META 结束
if (this.enableGenerateHTML == true && IsPublish == true)
{
string filePath = Server.MapPath(htmlFileName);
StreamWriter streamWriter = new StreamWriter(filePath, false, Encoding.UTF8);
streamWriter.Write(body);
streamWriter.Close();
}
Response.Write(body);
}
protected void Supervision()
{
string path = HttpContext.Current.Request.PhysicalPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath, string.Empty);
string key = string.Format("EnableSupervision_Website_SiteFiles_{0}", path.ToLower());
bool EnableSupervision = false;
if (HttpContext.Current.Cache[key] != null)
{
//如果有缓存就取出来
EnableSupervision = Convert.ToBoolean(HttpContext.Current.Cache[key]);
}
else
{
string sql = string.Format("select EnableSupervision from dbo.Website_SiteFiles where [Path] = N'{0}'", path);
object returnValue = Kuqu.Component.SqlHelper.ExecuteScalar(Kuqu.Component.DataHelper.ConnectionString, CommandType.Text, sql);
try
{
EnableSupervision = Convert.ToBoolean(returnValue);
}
catch
{
EnableSupervision = false;
}
//如果没缓存就插一条
HttpContext.Current.Cache.Insert(key, EnableSupervision, null, DateTime.Now.AddYears(1), TimeSpan.Zero);
}
if (EnableSupervision)
{
string scriptWebLog = @"
<script type='text/javascript'>
if(typeof(PageMethods) == 'function') {window.setTimeout('setWebLog()',5000);}
function setWebLog(){
if(window.screen.availWidth>0)
{
PageMethods('setWebLog','','' ,'";
if (HttpContext.Current.Request.UrlReferrer != null)
{
scriptWebLog += StringHelper.Escape(HttpContext.Current.Request.UrlReferrer.ToString());
}
scriptWebLog += @"');
} }
</script>";
base.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptWebLog", scriptWebLog);
}
}
public void ExecuteAjaxRequest(string function, HttpContext context)
{
Response.Clear();
int formFieldCount = context.Request.Form.Count;
object[] arguments;
if (formFieldCount > 0)
arguments = new object[formFieldCount];
else
arguments = null;
Type cls = this.GetType().BaseType;
MethodInfo methodInfo = cls.GetMethod(function, BindingFlags.Public | BindingFlags.Static);
if (methodInfo == null)
{
cls = this.GetType().BaseType.BaseType;
methodInfo = cls.GetMethod(function, BindingFlags.Public | BindingFlags.Static);
}
ParameterInfo[] pars = methodInfo.GetParameters();
for (int i = 0; i < formFieldCount; i++)
{
switch (pars[i].ParameterType.FullName)
{
case "System.Int32":
arguments[i] = Convert.ToInt32(context.Request.Form[i]);
break;
case "System.Decimal":
arguments[i] = Convert.ToDecimal(context.Request.Form[i]);
break;
case "System.String":
arguments[i] = StringHelper.dealWithSQLSecurity(context.Request.Form[i].ToString());
break;
default:
arguments[i] = StringHelper.dealWithSQLSecurity(context.Request.Form[i].ToString());
break;
}
}
//禁止使用.ToString()进行转换
string response = (string)cls.InvokeMember(function,
BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null, null, arguments);
Response.Write(response);
Response.End();
}
/// <summary>
/// 当前登录用户信息
/// </summary>
public Kuqu.Instance.User_Users LoginedUser
{
get { return Kuqu.Application.User_Users.LoginedUserInfo; }
}
/// <summary>
/// 读取根目录路径
/// </summary>
public string ApplicationRoot
{
get
{
return HttpContext.Current.Request.ApplicationPath;
}
}
protected string description = string.Empty;
protected string keywords = string.Empty;
public static void setWebLog(string HttpReferrer)
{
//判断缓存里是否有这个东东。
string Url = StringHelper.dealWithSQLSecurity(HttpContext.Current.Request.UrlReferrer.ToString());
string sql = "测试";
int ObjectId = 0;
//http://192.168.1.12/Resource/Detail.aspx?Id=74674
if (Url.IndexOf("=") > 0)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"=[\d]+");
if (reg.IsMatch(Url))
{
ObjectId = Convert.ToInt32(reg.Match(Url).ToString().Replace("=", ""));
}
}
int ChannelId = 0;
Dictionary<string, string> Channels = Kuqu.Application.Website_Channels.Channels;
//根据特征值获取ChannelId
foreach (string Key in Channels.Keys)
{
if (Url.ToUpper().IndexOf(Channels[Key].ToUpper()) > 0)
{
ChannelId = Convert.ToInt32(Key);
break;
}
}
int UserId = 0;
if (Kuqu.Application.User_Users.LoginedUserInfo != null)
{
UserId = Kuqu.Application.User_Users.LoginedUserInfo.UserId;
}
string UserIp = HttpContext.Current.Request.UserHostAddress;
// 1,ObjectId 对象(文章/被访问用户)编号
//2,ChannelId 频道编号
//3,UserId 访问用户编号
//4,VisitTime 起始时间
//5,Url 地址
//6,HttpReferrer 前页地址
//7,UserIp 用户IP
HttpReferrer = StringHelper.dealWithSQLSecurity(HttpReferrer);
sql = @"if not exists(select * from Website_ViewCount where ObjectId={0} and ChannelId={1} and UserId={2} and datediff(day,VisitTime,getdate()) <1 )
INSERT INTO[Website_ViewCount]([ObjectId],[ChannelId],[UserId],[Url],[HttpReferrer],[UserIp])VALUES({0},{1},{2},'{3}','{4}','{5}')";
sql = string.Format(sql, ObjectId, ChannelId, UserId, Url, HttpReferrer, UserIp);
SqlHelper.ExecuteNonQuery(DataHelper.ConnectionString, CommandType.Text, sql);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -