1 ServerPush
ServerPush表示服务器端推送,实际是浏览器一直请求服务器,服务器一直等待 直到找到一条数据后立即跳出while返回页面。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="file/jquery-1.11.2.js"></script> <script type="text/javascript"> //登录,一直请求服务器推送信息 var recv = function() { var me = $('#me').val().trim(); $.ajax({ type: 'post', url: 'ServerPushChat.ashx', data: { me: me, action: 'receive' }, success: function (data) { $('#dv').append($('<p>' + data.data.FROMUSERNAME + '对我说:' + data.data.MSG + '</p>')); //无论成功还是失败,都需要再次向服务器请求‘给哥一条消息’ recv(); }, error: function () { //alert('服务器错误'); recv(); } }); } $(function () { //点击登录,使服务器用serverPush推送信息 $('#btnLogin').click(function () { recv(); $(this).attr('disabled', 'disabled'); }); //发送,发送给服务器信息 $('#btnSend').click(function () { var me = $('#me').val().trim(); var toUserName = $('#toUserName').val().trim(); var msg = $('#msg').val().trim(); $.ajax({ type: 'post', url: 'ServerPushChat.ashx', data: { me: me, toUserName: toUserName, msg:msg,action: 'send' }, success: function (data) { $('#dv').append($('<p>我对' + data.data.TOUSERNAME + '说:' + data.data.MSG + '</p>')); }, error: function () { alert('服务器错误'); } }); }); }) </script> </head> <body> <div> 我:<input type="text" id="me" /><input type="button" id="btnLogin" value="登录" /><br /> 你:<input type="text" id="toUserName" /><br /> 内容:<input type="text" id="msg" /><input type="button" id="btnSend" value="发送" /><br /> </div> <div id="dv"> </div> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using Console_Core.BLL; using Console_Core.Model; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Web; using System.Web.Script.Serialization; namespace Web_Cassini.Day6 { /// <summary> /// ServerPushChat 的摘要说明 /// </summary> public class ServerPushChat : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; string action = context.Request["action"]; if (action == "receive") { #region 请求服务器一直推送信息 string me = context.Request["me"]; //验证 -- while (true) { List<object> list = new MyORM_BLL().SelectModelByField(typeof(TC_SERVERPUSHCHAT), "TOUSERNAME='" + me + "'"); if (list.Count <= 0) { Thread.Sleep(500); continue; } else { TC_SERVERPUSHCHAT chat = list[0] as TC_SERVERPUSHCHAT; bool flag = new MyORM_BLL().DeleteModelById(typeof(TC_SERVERPUSHCHAT), (int)chat.ID); //把客户接收到的数据删除 context.Response.Write(new JavaScriptSerializer().Serialize(new { status = "ok", data = chat, msg = "接收到一条数据" })); break; //接收到一条数据 立即跳出while 返回页面 } } #endregion } else if (action == "send") { string me = context.Request["me"]; string toUserName = context.Request["toUserName"]; string msg = context.Request["msg"]; //验证 --- TC_SERVERPUSHCHAT chat=new TC_SERVERPUSHCHAT(); chat.FROMUSERNAME=me; chat.TOUSERNAME=toUserName; chat.MSG=msg; bool flag = new MyORM_BLL().InsertModel(chat, "SE_TC_STUDENT"); context.Response.Write(new JavaScriptSerializer().Serialize(new { status = "ok", data = chat, msg = "发送成功" })); } else { throw new Exception("action错误:"+action); } } public bool IsReusable { get { return false; } } } }
2 Global
注意:html静态页面不会启动服务器。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "第一次访问网站时调用:" + DateTime.Now.ToString() + "Application_Start" + " "); } protected void Session_Start(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "操作Session时调用:" + DateTime.Now.ToString() + "Session_Start" + " "); } protected void Application_BeginRequest(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "向服务器发送请求时调用:" + DateTime.Now.ToString() + "Application_BeginRequest :" + Context.Request.RawUrl + " "); } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "应用程序发送异常时调用:" + DateTime.Now.ToString() + "Application_Error :" + Context.Error.ToString() + " "); } protected void Session_End(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "进程内session(InPro)过期时调用:" + DateTime.Now.ToString() + "Session_End" + " "); } protected void Application_End(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "网站停止时调用:" + DateTime.Now.ToString() + "Application_End" + " "); } }
3 Url Rewrite
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Application_BeginRequest(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "向服务器发送请求时调用:" + DateTime.Now.ToString() + "Application_BeginRequest :" + Context.Request.RawUrl + " "); //http://localhost:13588/Day7/WebForm_rewrite.aspx?id=6 //File.AppendAllText("F:\testGlobal.txt",Context.Request.Path); //path: /Day7/WebForm_rewrite.aspx path //RawUrl: /Day7/WebForm_rewrite.aspx?id=8 url // /Day7/WebForm_rewrite-8.aspx Match match = Regex.Match(Context.Request.RawUrl, @"^/Day7/WebForm_rewrite-(d+).aspx$"); if(match.Success) { Context.RewritePath("/Day7/WebForm_rewrite.aspx?id=" + match.Groups[1].Value); } }
4 Application
Application是全局变量,真个程序中都适用。
原理:在设值时因为是全局变量,需要保证只有一个用户在操作,所以需要加锁,直到设值结束才解锁,这样使得同一时间只能一个访问它,其他的都卡死。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//application1.aspx protected void Button1_Click(object sender, EventArgs e) { //application是全局变量,访问设值时必须加锁 Application.Lock(); Application.Set("rupeng", "very good"); Application.UnLock(); } //application2.ashx protected void Button1_Click(object sender, EventArgs e) { //application的取值不需要加锁 Button1.Text = Application.Get("rupeng").ToString(); }
在Global的Application_BeginRequest 中统计网站的访问次数:
(1)不能统计html静态页面;(2)加锁过程会卡死其他用户(实际中基本不用)。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Application_BeginRequest(object sender, EventArgs e) { File.AppendAllText("F:\testGlobal.txt", "向服务器发送请求时调用:" + DateTime.Now.ToString() + "Application_BeginRequest :" + Context.Request.RawUrl + " "); #region Url Rewrite Match match = Regex.Match(Context.Request.RawUrl, @"^/Day7/WebForm_rewrite-(d+).aspx$"); if (match.Success) { Context.RewritePath("/Day7/WebForm_rewrite.aspx?id=" + match.Groups[1].Value); } #endregion #region 统计网站的访问次数 //1 不能统计html等静态页面的访问次数 //2 一旦某人访问,在设值期间被加锁,则其他人无法访问 会卡死 直到上一访问结束为止。(实际中基本不用) int? count = (int?)Application.Get("count"); if (count == null) { count = 0; } count++; //加锁 设值 Application.Lock(); Application.Set("count", count); Application.UnLock(); #endregion }
5 Cache
缓存的原理:首先尝试从缓存中取,如果缓存中没有再从数据库中取,同时把取到的数据缓存起来,以便下次可以直接从缓存取,降低数据库的压力。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public partial class WebForm_cache : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { #region 缓存 List<object> list = (List<object>)HttpRuntime.Cache["tc_students"]; if (list == null || list.Count <= 0) //如果缓存中没有就从DB中获取 { List<object> listDB = new MyORM_BLL().SelectAllModel(typeof(TC_STUDENT)); if (listDB.Count > 0) //把数据库中获得的数据进行缓存 { HttpRuntime.Cache.Insert("tc_students", listDB, null, DateTime.Now.AddSeconds(30), TimeSpan.Zero); } list = listDB; } Repeater1.DataSource = list; Repeater1.DataBind(); #endregion } }
6 母版页
原理:在母版页中存在“占位符” --->
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
在使用母版页的WebForm中可以添加对应“占位符”的内容 --->
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h2>我也凑热闹 ContentPlaceHolder1</h2>
</asp:Content>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Master.master.cs" Inherits="Web_Cassini.Day7.Master" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> </head> <body> <div> <h1>关于我们|联系我们|加入我们</h1> </div> <form id="form1" runat="server"> <div> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> </form> <h1>提供方便和提供时间 版权所有</h1> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ Page Title="" Language="C#" MasterPageFile="~/Day7/Master.Master" AutoEventWireup="true" CodeBehind="useMaster1.aspx.cs" Inherits="Web_Cassini.Day7.useMaster1" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> <script type="text/javascript"> alert('我占用head这个坑'); </script> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <h2>我也凑热闹 ContentPlaceHolder1</h2> </asp:Content>
7 shtml
shtml比母版页更轻量级。SSI --> ServerSideInclude服务器端包含,主流浏览器都支持 ,IIS apache服务器时支持的。
shtml比母版页效率高得多,同时还会缓存起来,使下次不需要再分别加载。
<!--#include file="head.html"--> <h3>来得好,不如来得巧</h3> <!--#include file="foot.html"-->
8 IIS配置文档:
1、安装IIS。控制面板→程序→打开关闭Windows功能,Web管理服务和万维网服务都勾上。
2、部署网站:ASP.Net项目的发布:项目中点右键“发布”,选择“文件系统”,发布到一个文件夹下。
3、在IIS中新建网站,设定域名,这样多个域名可以放到一个IIS服务器上。需要绑定域名。
4、模拟域名,如果启用了UAC,则用管理员权限运行记事本,打开
C:WindowsSystem32driversetc下的hosts文件
做一下域名协议的欺骗。伪造一些域名出来。
5、如果报错报错“无法识别的属性“targetFramework”,则:
1)、把网站的应用程序池的.net framework版本改成“4.0”
2)、C:WindowsMicrosoft.NETFrameworkv4.0.30319下用管理员权限运行( aspnet_regiis.exe -i )
6、如果报错:未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序。
可能是因为访问Oracle的ODP是32位的,而系统是64(IIS是64位的),所以需要在iis中把该网站的引用程序池的高级设置的“启用32位应用程序”设置为true。
7、默认文档问题,让用户访问www.web2.com的时候其实是访问www.web2.com/index.apsx:如果用户没有指定要访问哪个文件,则从上向下,匹配到谁,谁就是默认文档。
注意:
MSSQL的windows身份登录在IIS运行的问题:
IIS是以windows服务运行,即不登录的时候已经在运行。
由于windows服务默认不是当前用户运行的,那么IIS的中运行的程序也不是以当前用户名运行的
,因此asp.net程序运行所采用的用户名不是sql server的管理员。
因此无法用“集成身份验证“登录SQL Server。