日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

ASP.Net零碎

發(fā)布時(shí)間:2025/4/5 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.Net零碎 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ASP.Net零碎

ServerPush

什么是ServerPush,服務(wù)器向客戶端瀏覽器“推送”,其實(shí)就是“長(zhǎng)連接”。 只有瀏覽器請(qǐng)求服務(wù)器端,服務(wù)器端才有給瀏覽器響應(yīng)數(shù)據(jù),不會(huì)主動(dòng)向?yàn)g覽器推送數(shù)據(jù),這樣是安全考慮,也是提高服務(wù)器的性能考慮。如果要服務(wù)器向?yàn)g覽器推送數(shù)據(jù),則需要使用ServerPush等技術(shù)模擬實(shí)現(xiàn)。 通過兩個(gè)頁(yè)面互相發(fā)送消息來(lái)實(shí)現(xiàn),消息放到數(shù)據(jù)庫(kù)。 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title><script src="jquery-2.1.1.js"></script><script type="text/javascript">$(function () {$("#send").click(function () {var me = $("#me").val();var tousername = $("#tousername").val();var msgs = $("#msgs").val();$.ajax({type: "post", url: "ServerPush.ashx",data: { action: "send", me: me, tousername: tousername, msgs: msgs },success: function (data) {if (data.Status =="ok" ){$("#ulMs").append($("<li>我對(duì)" + tousername + "說(shuō):" + msgs + "</li>"));$("#msgs").val("");}else {alert("發(fā)送出錯(cuò),返回報(bào)文無(wú)法識(shí)別");}},error: function () {alert("發(fā)送出錯(cuò)");}})})$("#lode").click(function () {var me = $("#me").val();$.ajax({type: "post", url: "ServerPush.ashx", data: { action: "serve", me: me },success: function (data) {$("#ulMs").append($("<li>" + data.Fromsername + "我對(duì)說(shuō):" + data.Msga + "</li>"));},error: function () {alert("發(fā)送出錯(cuò)");}})})})</script></head> <body>發(fā)送者:<input type="text" id="me" /><input type="button" id="lode" value="登陸" /><br />接受者<input type="text" id="tousername" />說(shuō):<input type="text" id="msgs" /><input type="button" id="send" value="發(fā)送" /><ul id="ulMs"></ul> </body>

?

//Id, Tousername, Fromusername, Msgspublic void ProcessRequest(HttpContext context){context.Response.ContentType = "application/json";string action = context.Request["action"];//判斷是接受還是發(fā)送if (action == "send"){string me = context.Request["me"];string tousername = context.Request["tousername"];string msg = context.Request["msgs"];SQLHelper.SqlEffectRow("insert into Msg(Fromusername,Tousername,Msgs) values(@FromUserName,@ToUserName,@Msg)", new SqlParameter("@FromUserName", me), new SqlParameter("@ToUserName", tousername), new SqlParameter("@Msg", msg));//新增消息context.Response.Write(new JavaScriptSerializer().Serialize(new{Status="ok"}) );}else if(action=="serve"){string me = context.Request["me"];while (true){DataTable Dt = SQLHelper.SQLDataTable("select top 1 * from Msg where Tousername=@Tousername", new SqlParameter("@Tousername", me));if (Dt.Rows.Count <= 0){Thread.Sleep(500);//休息500毫秒,減輕服務(wù)器壓力continue;}else{int id = (int)Dt.Rows[0]["Id"];string fromsername = (string)Dt.Rows[0]["Fromusername"];string msgs = (string)Dt.Rows[0]["Msgs"];SQLHelper.SqlEffectRow("delete from Msg where Id=@Id", new SqlParameter("@Id", id));//保存后刪除該條消息,避免重復(fù)var data = new { Fromsername = fromsername, Msga = msgs };string json = new JavaScriptSerializer().Serialize(data);//json context.Response.Write(json);break;}}}

?Global

Session_Start()和Session_End(),進(jìn)程外Session不會(huì)觸發(fā)Session_End()。重點(diǎn):Application_Start、Application_BeginRequest、Application_Error。 UrlRewrite: View.aspx?id=1→View-1.aspx 在BeginRequest中獲取請(qǐng)求的url( HttpContext.Current.Request.RawUrl ),生成真正的地址( Context.RewritePath ()) 靜態(tài)文件等默認(rèn)是不經(jīng)過asp.net引擎處理的,因此不會(huì)經(jīng)過Global。 1 namespace Web1 2 { 3 public class Global : System.Web.HttpApplication 4 { 5 //自從服務(wù)器啟動(dòng)起來(lái),網(wǎng)站第一次被訪問的時(shí)候Application_Start執(zhí)行 6 protected void Application_Start(object sender, EventArgs e) 7 { 8 File.AppendAllText("d:\\1.txt", DateTime.Now+"Application_Start\r\n"); 9 } 10 11 //Session啟動(dòng)時(shí) 12 protected void Session_Start(object sender, EventArgs e) 13 { 14 File.AppendAllText("d:\\1.txt", DateTime.Now + "Session_Start\r\n"); 15 } 16 17 //當(dāng)一個(gè)請(qǐng)求過來(lái)的時(shí)候 18 //html等靜態(tài)文件是iis直接把文件給到瀏覽器,不經(jīng)過asp.net引擎的處理。 19 //所以不會(huì)調(diào)用Application_BeginRequest方法 20 protected void Application_BeginRequest(object sender, EventArgs e) 21 { 22 //即使用戶訪問一個(gè)不存在的頁(yè)面,那么Application_BeginRequest也會(huì)被調(diào)用 23 24 File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_BeginRequest:"+ 25 Context.Request.RawUrl + "\r\n"); 26 //Context.RewritePath("WebExcel.html");//請(qǐng)求重寫在服務(wù)器內(nèi)部發(fā)生 27 //File.AppendAllText("d:\\1.txt", DateTime.Now + "Context.Request.Path:" + 28 //Context.Request.Path + "\r\n"); 29 30 int? count =(int?)Application.Get("Count"); 31 if (count == null) 32 { 33 count = 1; 34 } 35 count++; 36 Application.Lock(); 37 Application.Set("Count", count); 38 Application.UnLock(); 39 40 //Url重寫:UrlRewrite。ViewPerson-1.aspx 41 Match match = Regex.Match(Context.Request.Path, @"^/ViewPerson\-(\d+)\.aspx$"); 42 if (match.Success) 43 { 44 string id = match.Groups[1].Value; 45 Context.RewritePath("/ViewPerson.aspx?id="+id); 46 } 47 } 48 49 protected void Application_AuthenticateRequest(object sender, EventArgs e) 50 { 51 52 } 53 54 //程序中發(fā)生未處理異常 55 protected void Application_Error(object sender, EventArgs e) 56 { 57 File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_Error:"+ 58 Context.Error + "\r\n"); 59 } 60 61 //(*)Session過期(只有進(jìn)程內(nèi)Session,也就是InProc過期的時(shí)候才會(huì)調(diào)用Session_End) 62 protected void Session_End(object sender, EventArgs e) 63 { 64 File.AppendAllText("d:\\1.txt", DateTime.Now + "Session_End\r\n"); 65 } 66 67 protected void Application_End(object sender, EventArgs e) 68 { 69 File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_End\r\n"); 70 } 71 } 72 } View Code

UrlRewrite

1 //當(dāng)一個(gè)請(qǐng)求過來(lái)的時(shí)候 2 //html等靜態(tài)文件是iis直接把文件給到瀏覽器,不經(jīng)過asp.net引擎的處理。 3 //所以不會(huì)調(diào)用Application_BeginRequest方法 4 protected void Application_BeginRequest(object sender, EventArgs e) 5 { 6 //即使用戶訪問一個(gè)不存在的頁(yè)面,那么Application_BeginRequest也會(huì)被調(diào)用 7 8 File.AppendAllText("d:\\1.txt", DateTime.Now + "Application_BeginRequest:"+ 9 Context.Request.RawUrl + "\r\n"); 10 //Context.RewritePath("WebExcel.html");//請(qǐng)求重寫在服務(wù)器內(nèi)部發(fā)生 11 //File.AppendAllText("d:\\1.txt", DateTime.Now + "Context.Request.Path:" + 12 //Context.Request.Path + "\r\n"); 13 14 int? count =(int?)Application.Get("Count"); 15 if (count == null) 16 { 17 count = 1; 18 } 19 count++; 20 Application.Lock(); 21 Application.Set("Count", count); 22 Application.UnLock(); 23 24 //Url重寫:UrlRewrite。ViewPerson-1.aspx 25 Match match = Regex.Match(Context.Request.Path, @"^/ViewPerson\-(\d+)\.aspx$"); 26 if (match.Success) 27 { 28 string id = match.Groups[1].Value; 29 Context.RewritePath("/ViewPerson.aspx?id="+id); 30 } 31 }

?Application

Application是應(yīng)用全局對(duì)象,被全體共享。操作之前先Lock,操作完成后UnLock。 做網(wǎng)站開發(fā)盡量不要用Application,也很少有需要用它的時(shí)候。

ASP.Net緩存

把數(shù)據(jù)放到Cache中,在指定的時(shí)間內(nèi),可以直接從Cache中獲取,避免對(duì)數(shù)據(jù)庫(kù)等的壓力。 設(shè)置: HttpRuntime.Cache.Insert(CacheKey, objObject,null,absoluteExpiration,slidingExpiration); 1 //Cache是全局共享的 2 DataTable dt = (DataTable)HttpRuntime.Cache["persons"]; 3 if (dt == null)//如果Cache中沒有,再去數(shù)據(jù)庫(kù)中查詢 4 //這樣可以降低數(shù)據(jù)庫(kù)服務(wù)器的壓力 5 { 6 dt = SqlHelper.ExecuteQuery("select * from T_Persons"); 7 8 //存儲(chǔ)緩存,30秒后過期 9 HttpRuntime.Cache.Insert("persons", dt, null, 10 DateTime.Now.AddSeconds(30), TimeSpan.Zero); 11 } 12 13 Repeater1.DataSource = dt; 14 Repeater1.DataBind();

母版頁(yè)(*)和shtml

Webform的母版頁(yè)(MasterPage),使用母版頁(yè)的窗體。母版頁(yè)太笨重。 母版頁(yè)使用ContentPlaceHolder挖坑,“使用母版頁(yè)的窗體”用Content填坑 Shtml:ServerSideInclude(SSI),主流web服務(wù)器(iis、apache等)都支持。效率高,不需要經(jīng)過asp.net處理,輕量級(jí)。<!--#include file="info.htm"--> 1 <!--#include file="head.html"--> 2 正文 3 <!--#include file="foot.html"-->

?IIS配置

IIS配置文檔:

1、安裝IIS。控制面板→程序→打開關(guān)閉Windows功能,Web管理服務(wù)和萬(wàn)維網(wǎng)服務(wù)都勾上。

2、部署網(wǎng)站:ASP.Net項(xiàng)目的發(fā)布:項(xiàng)目中點(diǎn)右鍵“發(fā)布”,選擇“文件系統(tǒng)”,發(fā)布到一個(gè)文件夾下。

3、在IIS中新建網(wǎng)站,設(shè)定域名,這樣多個(gè)域名可以放到一個(gè)IIS服務(wù)器上。需要綁定域名。

4、模擬域名,如果啟用了UAC,則用管理員權(quán)限運(yùn)行記事本,打開

C:\Windows\System32\drivers\etc下的hosts文件

做一下域名協(xié)議的欺騙。偽造一些域名出來(lái)。

5、如果報(bào)錯(cuò)報(bào)錯(cuò)“無(wú)法識(shí)別的屬性“targetFramework”,則:

1)、把網(wǎng)站的應(yīng)用程序池的.net framework版本改成“4.0”?

2)、C:\Windows\Microsoft.NET\Framework\v4.0.30319下用管理員權(quán)限運(yùn)行( aspnet_regiis.exe -i )

6、默認(rèn)文檔問題,讓用戶訪問www.web2.com的時(shí)候其實(shí)是訪問www.web2.com/index.apsx:如果用戶沒有指定要訪問哪個(gè)文件,則從上向下,匹配到誰(shuí),誰(shuí)就是默認(rèn)文檔。

7、MSSQL的Windows身份登錄在IIS運(yùn)行的問題。IIS是以Windows服務(wù)( Windows服務(wù)的特點(diǎn)是:系統(tǒng)不登錄的時(shí)候已經(jīng)在運(yùn)行)的形式運(yùn)行,由于Windows服務(wù)默認(rèn)不是用當(dāng)前用戶名運(yùn)行的,那么IIS也就不是用當(dāng)前用戶名運(yùn)行的,那么IIS的中運(yùn)行的程序也不是以當(dāng)前用戶名運(yùn)行的,因此asp.net程序運(yùn)行所采用的用戶名不是SQLServer的管理員用戶,因此無(wú)法用“集成身份驗(yàn)證”登陸SQLServer,只能用“用戶名密碼方式登陸”

轉(zhuǎn)載于:https://www.cnblogs.com/Tan-sir/p/4742250.html

總結(jié)

以上是生活随笔為你收集整理的ASP.Net零碎的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。