Asp.Net Core(.net内核)
Asp.Net Core(.net內(nèi)核)
//----------------Day1----------------
一章?? ?Web基本原理
1節(jié)
課程說明
web窗體--設(shè)計(jì)界面--加法
使用Chrome
2節(jié)
瀏覽器與服務(wù)器的交互
登陸如鵬網(wǎng)時(shí)--工具---headers--Form Data中用戶名和密碼
Response返回的信息
www.rupeng.com---status code 301(重定向首頁)
瀏覽器向服務(wù)器發(fā)送請(qǐng)求,服務(wù)器處理之后,返回給瀏覽器(html)
3節(jié)
Socket原理和編寫控制臺(tái)瀏覽器代碼
Socket是進(jìn)行網(wǎng)路通訊的技術(shù)
qq用戶發(fā)送的信息通過Socket把信息發(fā)送給qqServer,qqServer返回給用戶
Socket原理性代碼: (TCP UDP兩種方式)
1、編寫“控制臺(tái)瀏覽器”
//把本機(jī)網(wǎng)址的html寫入socket
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);//TCP、UDP。
socket.Connect(new DnsEndPoint("127.0.0.1", 8080));//連接服務(wù)器。http協(xié)議默認(rèn)的端口號(hào)是80。每個(gè)服務(wù)器軟件監(jiān)聽一個(gè)端口(別的軟件就不能監(jiān)聽這個(gè)端口了),發(fā)送給這個(gè)端口的數(shù)據(jù)只會(huì)被這個(gè)服務(wù)器軟件接收到。
using (NetworkStream netStream = new NetworkStream(socket))//讀寫socket通訊數(shù)據(jù)的流
using (StreamWriter writer = new StreamWriter(netStream))
{
??? writer.WriteLine("GET /index.html HTTP/1.1");//每一行指令都回車一下
??? writer.WriteLine("Host: 127.0.0.1:8080");
??? writer.WriteLine();//空行回車,表示指令結(jié)束
}
//從socket讀取html
using (NetworkStream netStream = new NetworkStream(socket))
using (StreamReader reader = new StreamReader(netStream))
{
??? string line;
??? while ((line = reader.ReadLine())!=null)
??? {
??????? Console.WriteLine(line);?????????????????? ?
??? }
}
socket.Disconnect(false);
4節(jié)
編寫"網(wǎng)站服務(wù)器"
//自己寫的最簡(jiǎn)單的webservice
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//宿舍大媽
??????????? serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
??????????? serverSocket.Listen(10);
??????????? while (true)
??????????? {
??????????????? Console.WriteLine("等著請(qǐng)求");
??????????????? Socket socket = serverSocket.Accept();//男女通訊通道。返回大媽給你的這個(gè)人的通訊通道
??????????????? Console.WriteLine("來了請(qǐng)求");
??????????????? using(NetworkStream stream = new NetworkStream(socket))
??????????????? using (StreamReader reader = new StreamReader(stream))
??????????????? {
?? ??? ??? ?//讀取出第一行
??????????????????? string line;
??????????????????? while ((line = reader.ReadLine()) != null)
??????????????????? {
??????????????????????? Console.WriteLine(line);
??????????????????????? if (line.Length <= 0)
??????????????????????? {
??????????????????????????? break;//遇到空行了,請(qǐng)求結(jié)束了不用再等了
??????????????????????????? //如果不break,就一直卡在ReadLine()等著瀏覽器發(fā)后續(xù)的數(shù)據(jù)
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? using (NetworkStream stream = new NetworkStream(socket))
??????????????? using(StreamWriter writer = new StreamWriter(stream))
??????????????? {
??????????????????? writer.WriteLine("HTTP/1.1 200 OK");
??????????????????? writer.WriteLine();
??????????????????? writer.WriteLine("welcome to rupeng.com");
??????????????? }
??????????????? socket.Disconnect(false);
??????????? }
5節(jié)
編寫"網(wǎng)站服務(wù)器" 范虎請(qǐng)求的頁面
//自己寫的最簡(jiǎn)單的webservice
namespace MyWeb服務(wù)器2
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//宿舍大媽
??????????? serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
??????????? serverSocket.Listen(10);
??????????? while (true)
??????????? {
??????????????? Console.WriteLine("等著請(qǐng)求");
??????????????? Socket socket = serverSocket.Accept();//男女通訊通道。返回大媽給你的這個(gè)人的通訊通道
??????????????? Console.WriteLine("來了請(qǐng)求");
??????????????? string firstLine;
??????????????? using (NetworkStream stream = new NetworkStream(socket))
??????????????? using (StreamReader reader = new StreamReader(stream))
??????????????? {
??????????????????? firstLine = reader.ReadLine();//讀取GET /1.htm HTTP/1.1
??????????????????? string line;
??????????????????? while ((line = reader.ReadLine()) != null)
??????????????????? {
??????????????????????? Console.WriteLine(line);
??????????????????????? if (line.Length <= 0)
??????????????????????? {
??????????????????????????? break;//遇到空行了,請(qǐng)求結(jié)束了不用再等了
??????????????????????????? //如果不break,就一直卡在ReadLine()等著瀏覽器發(fā)后續(xù)的數(shù)據(jù)
??????????????????????? }
??????????????????? }
??????????????? }
?????????????? // Regex.Match(firstLine, "GET (.+) HTTP/1\\.1");
??????????????? string[] strs = firstLine.Split(' ');
??????????????? string url = strs[1];///分析出文件名1.htm
??????????????? Console.WriteLine("url="+url);
??????????????? using (NetworkStream stream = new NetworkStream(socket))
??????????????? using (StreamWriter writer = new StreamWriter(stream))
??????????????? {
??????????????????? string filePath = @"F:\快盤\NextBig\NET課程\ASP.net\myweb服務(wù)器" + url;
??????????????????? Console.WriteLine("filePath=" + filePath);
??????????????????? if (File.Exists(filePath))
??????????????????? {
??????????????????????? writer.WriteLine("HTTP/1.1 200 OK");
??????????????????????? writer.WriteLine();
??????????????????????? string html =
??????????????????????????? File.ReadAllText(filePath);
??????????????????????? Console.WriteLine(html);
??????????????????????? writer.Write(html);
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? writer.WriteLine("HTTP/1.1 404 NOT FOUND");
??????????????????????? writer.WriteLine();
??????????????????????? writer.Write("沒有找到");
??????????????????? }
??????????????? }
??????????????? socket.Disconnect(false);
??????????? }
??????? }
??? }
}
6節(jié)
服務(wù)器動(dòng)態(tài)的計(jì)算
add?i=1&j=2
//把a(bǔ)dd?i=1&j=2轉(zhuǎn)換為字典
ParseQueryString(string str)
//分割后獲得name和value ,及i和1,加入返回dictonary中
//練習(xí):login?username='admin'&password='123'
namespace MyWeb服務(wù)器3
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//宿舍大媽
??????????? serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
??????????? serverSocket.Listen(10);
??????????? while (true)
??????????? {
??????????????? Console.WriteLine("等著請(qǐng)求");
??????????????? Socket socket = serverSocket.Accept();//男女通訊通道。返回大媽給你的這個(gè)人的通訊通道
??????????????? Console.WriteLine("來了請(qǐng)求");
??????????????? string firstLine;
??????????????? using (NetworkStream stream = new NetworkStream(socket))
??????????????? using (StreamReader reader = new StreamReader(stream))
??????????????? {
??????????????????? firstLine = reader.ReadLine();//讀取GET /1.htm HTTP/1.1
??????????????????? string line;
??????????????????? while ((line = reader.ReadLine()) != null)
??????????????????? {
??????????????????????? Console.WriteLine(line);
??????????????????????? if (line.Length <= 0)
??????????????????????? {
??????????????????????????? break;//遇到空行了,請(qǐng)求結(jié)束了不用再等了
??????????????????????????? //如果不break,就一直卡在ReadLine()等著瀏覽器發(fā)后續(xù)的數(shù)據(jù)
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? // Regex.Match(firstLine, "GET (.+) HTTP/1\\.1");
??????????????? string[] strs = firstLine.Split(' ');
??????????????? string url = strs[1];///分析出文件名add?i=1&j=2
??????????????? string[] strs2 = url.Split('?');
??????????????? string fileAction = strs[0];//add
??????????????? string qs = strs2[1];
??????????????? Dictionary<string, string> dict = ParseQueryString(qs);
??????????????? int i = Convert.ToInt32(dict["i"]);
??????????????? int j = Convert.ToInt32(dict["j"]);
??????????????? using (NetworkStream stream = new NetworkStream(socket))
??????????????? using (StreamWriter writer = new StreamWriter(stream))
??????????????? {
??????????????????? writer.WriteLine("HTTP/1.1 200 OK");
??????????????????? writer.WriteLine();
??????????????????? writer.WriteLine(i+j);
??????????????? }
??????????????? socket.Disconnect(false);
??????????? }
??????? }
??????? /// <summary>
??????? /// 把i=1&j=2&w=aaa轉(zhuǎn)換為一個(gè)Dictionary
??????? /// </summary>
??????? /// <param name="str"></param>
??????? /// <returns></returns>
??????? static Dictionary<string, string> ParseQueryString(string qs)
??????? {
??????????? Dictionary<string, string> dict = new Dictionary<string, string>();
??????????? string[] strs = qs.Split('&');
??????????? foreach (string str in strs)
??????????? {
??????????????? string[] kv = str.Split('=');
??????????????? string name = kv[0];
??????????????? string value = kv[1];
??????????????? dict.Add(name, value);
??????????? }
??????????? return dict;
??????? }
??? }
}
7節(jié)
服務(wù)器知道瀏覽器什么時(shí)候關(guān)閉嗎?
除非瀏覽器請(qǐng)求,否則服務(wù)器無法主動(dòng)向?yàn)g覽器發(fā)送數(shù)據(jù)
8節(jié)
web服務(wù)器和asp.net服務(wù)器能做什么
自己寫webservice: 太累 并發(fā)性 安全性
現(xiàn)成的webservice: Apache Ngix IIS
先用微軟的Cassini,然后才用IIS
//請(qǐng)求的處理,報(bào)文的處理,參數(shù)的處理,用現(xiàn)成的Web服務(wù)器
新建Web--一旦處理程序
9節(jié)
HttpHandler(ashx)
ashx下面有個(gè)ashx.cs就是C#代碼,ashx是微軟傻瓜化的結(jié)果
當(dāng)用戶訪問test1.ashx時(shí),服務(wù)器調(diào)用ashx.cs中test1的ProcessRequest方法
項(xiàng)目--屬性--Web--服務(wù)器--默認(rèn)勾上了IIS Express(IIS簡(jiǎn)化版) //推薦使用IIS Web服務(wù)器--報(bào)錯(cuò)目錄--可以直接使用地址
項(xiàng)目--屬性--Web--特定頁--把頁面地址寫入進(jìn)去
選擇一個(gè)瀏覽器--選擇瀏覽器的安裝目錄
調(diào)試時(shí)不能修改
雖然停止調(diào)試了,只要IIS還在運(yùn)行的,改了代碼后重新生成,就可以直接刷新訪問,html不用生成也可以訪問
10節(jié)
context.Response.ContentType = "text/html";
??????????? string username = context.Request["username"];
??????????? string password = context.Request["password"];
??????????? context.Response.Write("<html><head></head><body>");
??????????? if (username == "admin" && password == "123")
??????????? {
??????????????? context.Response.Write("<h1>歡迎光臨</h1>");
??????????? }
??????????? else
??????????? {
??????????????? context.Response.Write("<img src='3.jpg'/>");
??????????? }
11節(jié)
表單提交
重復(fù)的name也會(huì)被提交,沒有name就不會(huì)提交給服務(wù)器
input textarea select 只有這3中屬性的value值才能被提交給web服務(wù)器
submit比較特殊,只有被點(diǎn)擊的submit的name和value才會(huì)提交給服務(wù)器,其他的不會(huì)提交給服務(wù)器
對(duì)于select是選中的那個(gè)select才提交給服務(wù)器
//----------------Day2-------------
二章?? ?一般處理程序
12節(jié)
http協(xié)議
Restart、Reinstall、Reboot
favicon.ico 就是一個(gè)默認(rèn)的圖標(biāo),瀏覽器每次訪問,服務(wù)器都嘗試去訪問這個(gè)網(wǎng)頁,如果存在就顯示這個(gè)圖標(biāo)
后綴名是可以騙人的
瀏覽器不知道服務(wù)器內(nèi)部發(fā)生了什么,只接收結(jié)果,返回什么就顯示什么
連接Connection :瀏覽器與服務(wù)器之間傳輸數(shù)據(jù)的通道。http協(xié)議在請(qǐng)求期間還是支持保持連接Keep-Alice的,但是結(jié)束還是會(huì)斷開連接
請(qǐng)求Request
處理Process
響應(yīng)Response
13節(jié)
請(qǐng)求
瀏覽器就是騙人的: 瀏覽器發(fā)出的所有請(qǐng)求都是可以被偽造的,一定不能信任瀏覽器的請(qǐng)求
User-Agent:UA 用戶代理,代理用戶發(fā)出http請(qǐng)求的,就是一些當(dāng)前瀏覽器信息
Referer:這個(gè)請(qǐng)求來自于那個(gè)網(wǎng)頁
Accept-Encoding:服務(wù)器支持什么壓縮算法
Accept-Language:該瀏覽器支持哪種語言
響應(yīng)
服務(wù)器也是可以騙人的,可以欺騙黑客
HTTP/1.0 200 OK? //200返回的狀態(tài)碼,如果為404就是not found //500為internal server error //跳出"黃頁",可以根據(jù)報(bào)錯(cuò)信息,找到出現(xiàn)錯(cuò)誤的異常
Content-Type: 服務(wù)器返回的是什么類型的數(shù)據(jù) (后綴名是可以造假的) charset=utf-8;報(bào)文體采用的編碼
Accept-Rangge: 服務(wù)器是否支持?jǐn)帱c(diǎn)續(xù)傳
Server: 哪種服務(wù)器
Date:返回日期
Content-Length:內(nèi)容長(zhǎng)度
HTTP/1.0 302 Found //302重定向到Localing的頁面
context.Response.Redirect(); //重定向
HTTP/1.0 304 Not Modified // 表示沒有修改,從緩存取,沒有報(bào)文體
If-Modifed-Since:保存的修改日期,再次訪問表示沒有修改,如果沒有修改就直接重緩存中加載數(shù)據(jù)的
狀態(tài)碼 -->
2XX:表示沒問題
3XX: 需要瀏覽器干點(diǎn)啥
4XX:瀏覽器提交的數(shù)據(jù)有問題(客服端的訪問未授權(quán))
5XX: 服務(wù)器錯(cuò)誤
響應(yīng)碼:
“200” : OK;
“302” : Found 暫時(shí)轉(zhuǎn)移,用于重定向, Response.Redirect()會(huì)讓瀏覽器再請(qǐng)求一次重定向的地址,重定向的請(qǐng)求是Get方式;
"404" : Not Found 未找到。
500 服務(wù)器錯(cuò)誤(一般服務(wù)器出現(xiàn)異常),通過報(bào)錯(cuò)信息找出異常的點(diǎn)。
403:客戶端訪問未被授權(quán)。
304(服務(wù)器把文件的修改日期通過Last-Modified返回給瀏覽器,瀏覽器緩存這個(gè)文件,下次向服務(wù)器請(qǐng)求這個(gè)文件的時(shí)候,通過If-Modified-Since問服務(wù)器說“我本地的文件的修改日期是。。。”,服務(wù)器端如果發(fā)現(xiàn)文件還是那個(gè)文件,則告訴瀏覽器(304 Not Modified)文件沒修改,還用本地的吧。ctrl+f5)。
2xx:沒問題;3xx代表瀏覽器需要干點(diǎn)啥;4***瀏覽器的問題;5xx服務(wù)器錯(cuò)誤
14節(jié)
Get和Post的區(qū)別
get把數(shù)據(jù)放在地址欄,post把數(shù)據(jù)放到報(bào)文體中,提交大數(shù)據(jù),傳密碼安全
對(duì)于post請(qǐng)求只要在地址欄中輸入一個(gè)網(wǎng)址回車就是get請(qǐng)求;刷新,重新發(fā)出post請(qǐng)求,部分瀏覽器會(huì)提示是否重新提交
反編譯器搜索httpRequest這個(gè)類
15節(jié)
ASP.Net內(nèi)核幾大對(duì)象HttpContext;
context.Request對(duì)象
請(qǐng)求參數(shù)都是字符串,因?yàn)閔ttp協(xié)議是string
只要瀏覽器沒提交,服務(wù)器就獲取不了
//HttpContext和當(dāng)前線程相關(guān)的對(duì)象,在子線程中拿不到這個(gè)對(duì)象
//1
void Test(HttpContext context)
{
?? ?if() context.Request["a"]不存在
}
//2
void Test()
{
?HttpContext context=HttpContext.current;
?if() context.Request["a"]不存在
}
//3 post
string name=context.Request.Form["name"]; //只獲得通過報(bào)文體傳輸?shù)膮?shù)
//4 get
string name=context.Request.QueryString["name"]; //只獲取通過get方式獲得參數(shù)
//5
context.Response.Write("'+context.Request.Browser.Browser+'\n"); //返回打印瀏覽器的瀏覽器名 Platform? Version
i < context.Request.Headers.AllKeys.Length
string k=context.Request.Headers.AllKeys[i];
string v=context.Request.Headers[k];
namespace Web1
{
??? /// <summary>
??? /// RequestTest1 的摘要說明
??? /// </summary>
??? public class RequestTest1 : IHttpHandler
??? {
??????? public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/plain";
??????????? //context.Request.Form["name"]獲得通過報(bào)文體傳輸?shù)膮?shù)
?????????? // string name = context.Request.Form["name"];
??????????? //string age = context.Request.Form["age"];//拿到的都是String,Convert.ToInt32()
??????????? //context.Request.QueryString獲得GET方式中“GET /Test.ashx?id=5 HTTP/1.1”中的
??????????? //"id=5"值
??????????? //string name = context.Request.QueryString["name"];
??????????? //string age = context.Request.QueryString["age"];
??????????? //context.Request["name"]無論是Get還是Post都能獲得
?????????? // string name = context.Request["name"];//[""]索引器
?????????? // string age = context.Request["age"];
??????????? context.Response.Write("" + context.Request.Browser.Browser+ "\n");
??????????? context.Response.Write("" + context.Request.Browser.Platform + "\n");
??????????? context.Response.Write("" + context.Request.Browser.Version + "\n");
??????????? context.Response.Write("------------------------\n");
??????????? for (int i = 0; i < context.Request.Headers.AllKeys.Length; i++)//Request.Headers請(qǐng)求報(bào)文頭
??????????? {
??????????????? string key = context.Request.Headers.AllKeys[i];
??????????????? string value = context.Request.Headers[key];
??????????????? context.Response.Write(key+"="+value+"\n");
??????????? }
??????????? context.Response.Write("------------------------\n");
??????????? context.Response.Write(context.Request.HttpMethod + "\n");
??????????? //context.Response.Write(context.Request.InputStream);//請(qǐng)求報(bào)文體的流
??????????? context.Response.Write(context.Request.Path + "\n");
??????????? context.Response.Write(context.Request.QueryString + "\n");
??????????? context.Response.Write(context.Request.PhysicalPath + "\n");//被請(qǐng)求的文件的服務(wù)器上的物理路徑
??????????? context.Response.Write(context.Request.UserAgent + "\n");
??????????? context.Response.Write(context.Request.UserHostAddress + "\n");//客戶端的IP地址
??????????? context.Response.Write(context.Request.UrlReferrer + "\n");
??????????? context.Response.Write(String.Join(",",context.Request.UserLanguages) + "\n");//瀏覽器支持什么語言
??????? }
??????? public bool IsReusable
??????? {
??????????? get
??????????? {
??????????????? return false;
??????????? }
??????? }
??? }
}
16節(jié)
context.Response對(duì)象
ContentType :
OutputStream :圖片,二進(jìn)制輸出流
Redirect:重定向
//性能并不是唯一追求的指標(biāo)
if(username為nullOrEmpty)
{
?? ?context.Response.Write("用戶名為空");
?? ?//return;
?? ?context.Response.End(); //終止httpHandler的執(zhí)行? //因?yàn)樗鼟伭艘粋€(gè)異常,所以沒有繼續(xù)執(zhí)行,可以用try catch抓住這個(gè)異常,因?yàn)楫惓L幚硇实?#xff0c;所以盡量不用response.end()
}
void Test()
{
?? ?HttpContext.Current.Respnse.Write("數(shù)據(jù)庫插入失敗");
?? ?//return; //只是終止Test()的執(zhí)行
?? ?HttpContext.Current.Response.End();
}
End()將當(dāng)前所有緩沖的輸出發(fā)送到客戶端,停止該頁的執(zhí)行。通過對(duì)End()進(jìn)行try,發(fā)現(xiàn)是是拋出了異常。所以End()之后的代碼就不會(huì)執(zhí)行了。
17節(jié)
context.Server對(duì)象
Server.Transfer:控制權(quán)的轉(zhuǎn)移
Server.Mapth:獲得絕對(duì)路徑
Server.HtmlEncode:HTML編碼解碼
Server.UrlEncode:url編碼解碼
context.Server.Transfer("sayhello?name=admin&age=18"); //有錯(cuò),以后講
FileInfo fi=new FileInfo(@"E:\s1.jpg"); //永遠(yuǎn)不用相對(duì)路勁,容易進(jìn)坑
context.Server.MapPath("~/3.jpg"); //獲得文件的絕對(duì)路徑,~表示網(wǎng)站的根目錄
string csCode="helllo List<T> list=new List<T>()";
context.Server.HtmlEncode(csCode);
context.Server.HtmlDecode(csCode);
string str="如鵬 12什么么";
context.Server.UrlEncode(str); //轉(zhuǎn)換為url編碼
Server是一個(gè)HttpServerUtility類型的對(duì)象,不是一個(gè)類名
MapPath:MapPath("~/a.htm")將虛擬路徑(~代表項(xiàng)目根目錄)轉(zhuǎn)換為磁盤上的絕對(duì)路徑,操作項(xiàng)目中的文件使用。
HtmlEncode、 HtmlDecode:HTML編碼解碼。Encode為的是把特殊字符轉(zhuǎn)義顯示,舉例子:"< 如鵬+網(wǎng)? rupeng.com>"
UrlEncode、 UrlDecode:url編碼解碼。漢字、特殊字符(空格、尖括號(hào))等通過Url傳遞的時(shí)候要編碼,舉例子“"<如鵬+網(wǎng)? rupeng.com>”
18節(jié)
ContentType = "image/jpg";
//瀏覽器是不知道服務(wù)器上有3.jpg的存在的,瀏覽器只是發(fā)請(qǐng)求,顯示圖片
//把文件流的數(shù)據(jù)拷貝到outputstream輸出流
//輸出一個(gè)動(dòng)態(tài)創(chuàng)建的圖片
//圖片中顯示訪問者瀏覽器信息
//把名字寫入泡妞證
//生成驗(yàn)證碼,動(dòng)態(tài)生成一200*50的圖片,顯示一個(gè)隨機(jī)的4位數(shù)
????????? //context.Response.ContentType = "image/jpeg";//image/gif image/png
?? ?
?? ?//1
??????????? /*
??????????? string filepath = context.Server.MapPath("~/3.jpg");
??????????? //瀏覽器是不知道服務(wù)器上有3.jpg的存在的,瀏覽器只是發(fā)請(qǐng)求,接收請(qǐng)求,顯示圖片
??????????? //別的一概不知
??????????? using (Stream inStream = File.OpenRead(filepath))//new FileStream(...);
??????????? {
??????????????? inStream.CopyTo(context.Response.OutputStream);
??????????? }*/
????????? ?
?? ?//2
??????????? /*
??????????? using (Bitmap bmp = new Bitmap(500, 500))//創(chuàng)建一個(gè)尺寸為500*500的內(nèi)存圖片
??????????? using (Graphics g = Graphics.FromImage(bmp))//得到圖片的畫布
??????????? {
??????????????? g.DrawString("如鵬網(wǎng)萬歲", new Font(FontFamily.GenericSerif, 30), Brushes.Red, 100, 100);//Font應(yīng)該被釋放
??????????????? g.DrawEllipse(Pens.Blue, 100, 100, 100, 100);
??????????????? //bmp.Save(@"d:\1.jpg", ImageFormat.Jpeg);
??????????????? //直接保存到網(wǎng)頁輸出流中
??????????????? bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
??????????? } */
?? ?//3
?? ??? ?// string name = context.Request["name"];
??????????? /*
??????????? using (Bitmap bmp = new Bitmap(500, 500))//創(chuàng)建一個(gè)尺寸為500*500的內(nèi)存圖片
??????????? using (Graphics g = Graphics.FromImage(bmp))//得到圖片的畫布
??????????? {
??????????????? g.DrawString(name+"萬歲", new Font(FontFamily.GenericSerif, 30), Brushes.Red, 100, 100);//Font應(yīng)該被釋放
??????????????? g.DrawEllipse(Pens.Blue, 100, 100, 100, 100);
??????????????? //bmp.Save(@"d:\1.jpg", ImageFormat.Jpeg);
??????????????? //直接保存到網(wǎng)頁輸出流中
??????????????? bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
??????????? }*/
??????????? /*
??????????? context.Response.ContentType = "image/jpeg";
??????????? using (Bitmap bmp = new Bitmap(500, 200))//創(chuàng)建一個(gè)尺寸為500*500的內(nèi)存圖片
??????????? using (Graphics g = Graphics.FromImage(bmp))//得到圖片的畫布
??????????? using (Font font = new Font(FontFamily.GenericSerif, 30))
??????????? {
??????????????? HttpRequest request = context.Request;
??????????????? g.DrawString("IP:" + request.UserHostAddress, font, Brushes.Red, 0, 0);
??????????????? g.DrawString("瀏覽器:" + request.Browser.Browser + request.Browser.Version, font, Brushes.Red, 0, 50);
??????????????? g.DrawString("操作系統(tǒng):" + request.Browser.Platform, font, Brushes.Red, 0, 100);
??????????????? bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//圖片保存到輸出流
??????????? }*/
??????????? context.Response.ContentType = "image/jpeg";
??????????? string name = context.Request["name"];
??????????? string imgFullPath = context.Server.MapPath("~/PaoNiuZheng.jpg");
??????????? using (Image bmp = Bitmap.FromFile(imgFullPath))//讀取一張已有的圖片
??????????? using (Graphics g = Graphics.FromImage(bmp))//得到圖片的畫布
??????????? using (Font font1 = new Font(FontFamily.GenericSerif, 12))
??????????? using (Font font2 = new Font(FontFamily.GenericSerif, 5))
??????????? {
??????????????? {
??????????????????? g.DrawString(name, font1, Brushes.Black, 125, 220);//Font應(yīng)該被釋放
??????????????????? g.DrawString(name, font2, Brushes.Black, 309, 55);//Font應(yīng)該被釋放
??????????????????? bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//圖片保存到輸出流
??????????????? }
??????????? }
??????????? //對(duì)瀏覽器來講,只知道“服務(wù)器哥哥給我返回了一個(gè)image/jpeg,正文就是圖片”
19節(jié)
文件下載
對(duì)于.ashx右鍵下載,其實(shí)是l向f發(fā)送http請(qǐng)求
??????????? context.Response.ContentType = "application/ms-excel";
??????????? /*
??????????? //增加Content-Disposition是告訴瀏覽器,這個(gè)返回的內(nèi)容是“附件形式”要給用戶保存
??????????? //filename是建議的文件名?????????? ?
??????????? context.Response.AddHeader("Content-Disposition", "attachment;filename=" +
??????????????? context.Server.UrlEncode("動(dòng)態(tài)文件.txt"));?? ?//-------------???---格式不對(duì)-----------------
??????????? DataTable dt = SqlHelper.ExecuteQuery("select * from t_persons");
??????????? foreach (DataRow row in dt.Rows)
??????????? {
??????????????? context.Response.Write(row["Name"]+"!"+row["Age"]+"\r\n");
??????????? }*/
??????????? context.Response.AddHeader("Content-Disposition", "attachment;filename=" +
??????????????? context.Server.UrlEncode("人員列表.xls"));
??????????? IWorkbook workbook = new HSSFWorkbook() ;//new XSSFWorkbook();//xlsx
??????????? ISheet sheet = workbook.CreateSheet("人員列表");
??????????? DataTable dt = SqlHelper.ExecuteQuery("select * from t_persons");
??????????? for (int i=0;i<dt.Rows.Count;i++)
??????????? {
??????????????? IRow excelRow = sheet.CreateRow(i);
??????????????? DataRow dataRow = dt.Rows[i];
??????????????? ICell cell0 = excelRow.CreateCell(0);
??????????????? cell0.SetCellValue((string)dataRow["Name"]);
??????????????? ICell cell1 = excelRow.CreateCell(1);
??????????????? cell1.SetCellValue((int)dataRow["Age"]);
??????????? }
??????????? workbook.Write(context.Response.OutputStream);
20節(jié)
文件上傳
post
enctype="multipart/form-data"
HttpPostFile
file1.SaveAs(全路徑);
//判斷文件大小,不能超過1M
//根據(jù)文件后綴判斷文件類型,ContentType可以偽造
??????????? context.Response.ContentType = "text/html";
??????????? HttpPostedFile file1 = context.Request.Files["file1"];//上傳的文件,可以是多個(gè)
??????????? HttpPostedFile filehaha = context.Request.Files["filehaha"];
??????????? string name = context.Request["name"];
??????????? //HttpPostedFile.FileName是文件名,通過查看報(bào)文發(fā)現(xiàn),FileName是瀏覽器提交過去的
??????????? if (file1.ContentLength > 1024 * 1024)
??????????? {
??????????????? context.Response.Write("文件不能超過1MB");
??????????????? return;
??????????? }
??????????? //為什么根據(jù)ContentType判斷文件類型不安全。
??????????? //因?yàn)镃ontentType是瀏覽器提交的,是可以造假的,文件內(nèi)容是病毒,filename是1.exe
??????????? //ContentType偽造成了"image/jpeg"。
??????????? /*
??????????? if(file1.ContentType!="image/jpeg"&&file1.ContentType!="image/gif"
???????????????? &&file1.ContentType!="image/png")
??????????? {
??????????????? context.Response.Write("文件類型不允許");
??????????????? return;
??????????? }*/
??????????? string fileExt = Path.GetExtension(file1.FileName);
??????????? if(fileExt!=".jpg"&&fileExt!=".gif"&&fileExt!=".png")
??????????? {
??????????????? context.Response.Write("文件類型不允許");
??????????????? return;
??????????? }
??????????? string localFileName = context.Server.MapPath("~/upload") + "/" + file1.FileName;
??????????? file1.SaveAs(localFileName);//把文件保存到服務(wù)器上
??????????? context.Response.Write(localFileName+"<br/>");
??????????? context.Response.Write(name);
??????? }
//判斷文件大小和類型,并在圖片上畫上一行字
??????????? context.Response.ContentType = "text/html";
??????????? HttpPostedFile file1 = context.Request.Files["file1"];
??????????? if (file1.ContentLength > 1024 * 1024)
??????????? {
??????????????? context.Response.Write("文件不能超過1MB");
??????????????? return;
??????????? }
??????????? string fileExt = Path.GetExtension(file1.FileName);
??????????? if (fileExt != ".jpg" && fileExt != ".jpeg")
??????????? {
??????????????? context.Response.Write("只允許jpg圖片");
??????????????? return;
??????????? }
??????????? //用戶上傳的文件盡量“不落地”,也就是不保存到本地,可以避免:上傳文件把服務(wù)器撐爆;
??????????? //上傳非法文件造成安全性問題。
??????????? //只能證明一段代碼有漏洞,不能證明代碼沒有漏洞。
??????????? using (Image img = Bitmap.FromStream(file1.InputStream))//**
??????????? {
??????????????? using (Graphics g = Graphics.FromImage(img))
??????????????? using (Font font = new Font(FontFamily.GenericSerif, 18))
??????????????? {
??????????????????? g.DrawString("如鵬網(wǎng) rupeng.com", font, Brushes.Red, 0, 0);
??????????????? }
??????????????? string filename = DateTime.Now.ToString("yyyyMMdd");//20150308
??????????????? img.Save(context.Server.MapPath("~/upload/" + filename + fileExt));
??????????? }
/----------------------Day3---------------------
三章?? ?ashx增刪改查
21節(jié)
作業(yè)講解
//Excel上傳
//保存文件到y(tǒng)yyy\mm\dd.png中
每次return輸出打印結(jié)束,都輸出html結(jié)束標(biāo)簽
if(file1.COntentLength<=0){未上傳文件};
以調(diào)試方式啟動(dòng),才可以進(jìn)行斷點(diǎn)調(diào)試
不需要?jiǎng)e人告訴結(jié)論,你試驗(yàn)的結(jié)果就是真理
IWrokbook workbook=WorkbookFactory.Create(file1.InputStream);
sheet.SheetName //獲得sheet名
i<sheet.LastRowNum 從i=1開始
j<excelRow.LastCellNum 從j=ecelRow.FirstCellNum開始
string fileFullPath=Path.Combine(dirFullPath,file1.FileName);
if(!Directory.Exists(dirFullPath)){就創(chuàng)建這個(gè)文件夾}
22節(jié)
模板文件
Id Name Age Gender
//從數(shù)據(jù)庫查詢Person,通過html拼接,在.ashx輸出信息
//html中的占位符@Name用.ashx中的值替換
??????? public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/html";
?? ???? //判斷id時(shí)否合法
??????????? int id = Convert.ToInt32(context.Request["id"]);
??????????? DataTable table =
??????????????? SqlHelper.ExecuteQuery("select * from T_Persons where Id=@Id", new SqlParameter("@Id", id));
??????????? if (table.Rows.Count <= 0)
??????????? {?????????????? ?
??????????????? return;
??????????? }
??????????? DataRow row = table.Rows[0];
??????????? string name = (string)row["Name"];
??????????? int age = (int)row["age"];
??????????? string pesronViewHtmlFileName = context.Server.MapPath("~/Day3/PersonView2.txt");
??????????? string personViewHtml = File.ReadAllText(pesronViewHtmlFileName);
??????????? personViewHtml = personViewHtml.Replace("@name", name).Replace("@age",age.ToString());
?????????? // personViewHtml = personViewHtml.Replace("@name", name);
??????????? //personViewHtml = personViewHtml.Replace("@age", age.ToString());
??????????? context.Response.Write(personViewHtml);
??????? }
23節(jié)
通過抽象提取方法優(yōu)化例子
模板優(yōu)化:如果報(bào)錯(cuò),輸出報(bào)錯(cuò)信息
"{msg}" --> 占位符
封裝:讀取虛擬路勁下的.html文件,返回html字符串 CommonHelper.cs
封裝:輸出錯(cuò)誤信息
?????? public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/html";
?? ???? //判斷id是否合法
??????????? string strId = context.Request["id"];
??????????? if (string.IsNullOrEmpty(strId))
??????????? {
????????????? //? string errorFileName = context.Server.MapPath("~/Day3/PersonViewError.html");
??????????????? //string errorHtml = File.ReadAllText(errorFileName);
?????????????? // string errorHtml = CommonHelper.ReadHtml("~/Day3/PersonViewError.html");
??????????????? //errorHtml = errorHtml.Replace("{msg}", "id不能為空!");
??????????????? //context.Response.Write(errorHtml);
??????????????? OutputError("id不能為空!");
??????????????? return;
??????????? }
??????????? int id = Convert.ToInt32(strId);
??????????? DataTable table =
??????????????? SqlHelper.ExecuteQuery("select * from T_Persons where Id=@Id", new SqlParameter("@Id", id));
??????????? if (table.Rows.Count <= 0)
??????????? {
?????????????? // string errorFileName = context.Server.MapPath("~/Day3/PersonViewError.html");
??????????????? //string errorHtml = File.ReadAllText(errorFileName);
????????????? //? string errorHtml = CommonHelper.ReadHtml("~/Day3/PersonViewError.html");
??????????????? //errorHtml = errorHtml.Replace("{msg}", "id不存在!");
??????????????? //context.Response.Write(errorHtml);
??????????????? OutputError("id不存在!");
??????????????? return;
??????????? }
??????????? DataRow row = table.Rows[0];
??????????? string name = (string)row["Name"];
??????????? int age = (int)row["age"];
??????????? //DRY:Don't Repeat YourSelf:別拷代碼
??????????? //string pesronViewHtmlFileName = context.Server.MapPath("~/Day3/PersonView2.txt");
?????????? // string personViewHtml = File.ReadAllText(pesronViewHtmlFileName);
??????????? string personViewHtml = CommonHelper.ReadHtml("~/Day3/PersonView2.txt");
??????????? personViewHtml = personViewHtml.Replace("@name", name).Replace("@age",age.ToString());
?????????? // personViewHtml = personViewHtml.Replace("@name", name);
??????????? //personViewHtml = personViewHtml.Replace("@age", age.ToString());
??????????? context.Response.Write(personViewHtml);
??????? }
??????? private void OutputError(string msg)
??????? {
??????????? string errorHtml = CommonHelper.ReadHtml("~/Day3/PersonViewError.html");
??????????? errorHtml = errorHtml.Replace("{msg}", msg);
??????????? HttpContext.Current.Response.Write(errorHtml);
??????? }
24節(jié)
T_Persons列表
<table>標(biāo)簽中不能出現(xiàn)文本,但是沒什么關(guān)系
<table>中{Persons}被.ashx替換
模板的好處是不用改C#代碼,顯示什么只需改.html就行了
//刪除
刪除之后重定向到列表頁面,發(fā)生了2次請(qǐng)求?? context.Response.Redirect("PersonList.ashx");//刪除完成后重定向回列表頁面
判斷是否要?jiǎng)h除,在js中去轉(zhuǎn)義,需要在C#中加上一個(gè) \"
?//1
?.Append("<td><td><a οnclick='return confirm(\"你真的要?jiǎng)h除嗎?\")' href='PersonDelete.ashx?id=")
?.Append(row["id"]).Append("'>刪除</a></td>");
?//2
?οnclick=\"if(!confirm('您確定要?jiǎng)h除嗎?')){return false;}\"
25節(jié)
新增和編輯
??????????? context.Response.ContentType = "text/html";
??????????? //PesonEditAddNew.ashx?action=addnew,新增
??????????? //PesonEditAddNew.ahsx?action=edit&id=1
??????????? string action = context.Request["action"];
??????????? string html = CommonHelper.ReadHtml("~/Day3/PersonEditAddNew.html");
??????????? if (action == "addnew")
??????????? {
??????????????? html = html.Replace("{actionName}", "新增").Replace("{name}", "").Replace("{age}", "18")
??????????????????? .Replace("{gender}", "checked");
??????????????? context.Response.Write(html);
??????????? }
??????????? else if (action == "edit")
??????????? {
??????????????? int id = Convert.ToInt32(context.Request["id"]);
??????????????? DataTable table = SqlHelper.ExecuteQuery("Select * from T_Persons where Id=@Id",
??????????????????? new SqlParameter("@Id",id));
??????????????? if (table.Rows.Count <= 0)
??????????????? {
??????????????????? CommonHelper.OutputError("沒找到id="+id+"的人員");
??????????????????? return;
??????????????? }
??????????????? if (table.Rows.Count > 1)
??????????????? {
??????????????????? CommonHelper.OutputError("找到多條id=" + id + "的人員");
??????????????????? return;
??????????????? }
??????????????? DataRow row = table.Rows[0];
??????????????? string name = (string)row["name"];
??????????????? int age = (int)row["age"];
??????????????? bool gender = (bool)row["gender"];
??????????????? html = html.Replace("{actionName}", "編輯").Replace("{name}", name).Replace("{age}", age.ToString())
??????????????????? .Replace("{gender}",gender?"checked":"");
?????????????? ?
??????????????? context.Response.Write(html);
??????????? }
??????????? else
??????????? {
??????????????? CommonHelper.OutputError("action錯(cuò)誤");
??????????? }
??????? }
26節(jié)
保存
只有瀏覽器提交的,服務(wù)器才能知道,action是addnew還是edit必須由瀏覽器提交
27節(jié)
公司的增刪改查(下拉列表)
T_Company( Id Name Address ManageId)
T_Boss( Id Name Age Gender)
sql語句:聯(lián)合查詢
//作業(yè):學(xué)生管理系統(tǒng)
?? ?//班級(jí)---List
?? ?public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/html";
??????????? //獲得所有班級(jí)
??????????? DataTable dtCla = SqlHelper.ExecuteQuery(@"select c.CId CId,c.CName CName,c.CClassroomNum CClassroomNum,t.TName TName
?????????????????????????????? from T_Classes c left join T_Teachers t on c.TId=t.TId");
??????????? if(dtCla.Rows.Count<=0)
??????????? {
??????????????? CommonHelper.OutputErrorHtml("沒有查到任何班級(jí)");
??????????? }
??????????? //遍歷班級(jí),拼接字符串<tr>
??????????? StringBuilder sb = new StringBuilder();
??????????? foreach(DataRow row in dtCla.Rows)
??????????? {
??????????????? sb.Append("<tr><td>").Append(row["CName"]).Append("</td><td>").Append(row["CClassroomNum"]).Append("</td><td>").Append(row["TName"]).Append("</td>");
??????????????? sb.Append("<td>").Append("<a οnclick=\"return confirm('您確定要?jiǎng)h除嗎?');\" href='Delete.ashx?id=").Append(row["CId"]).Append("'>刪除</a></td>");
??????????????? sb.Append("<td><a href='Edit.ashx?action=edit&id=").Append(row["CId"]).Append("'>修改</a></td>").Append("</tr>");
??????????? }
??????????? //獲得html并替換輸出
??????????? string htmlStr = CommonHelper.OutputHtml("~/Class/List.html");
??????????? string html = htmlStr.Replace("{classes}", sb.ToString());
??????????? context.Response.Write(html);
??????? }
?? ?//班級(jí)---Edit
?? ?public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/html";
??????????? string save = context.Request["save"];
??????????? string action = context.Request["action"];
??????????? //判斷直接范問還是保存
??????????? if(string.IsNullOrEmpty(save))
??????????? {
??????????????? //判斷新增還是編輯
??????????????? if(action=="addnew")
??????????????? {
??????????????????? //獲得所有的老師
??????????????????? DataTable dtTea = SqlHelper.ExecuteQuery("select * from T_Teachers");
??????????????????? StringBuilder sb = new StringBuilder();
??????????????????? foreach(DataRow row in dtTea.Rows)
??????????????????? {
??????????????????????? sb.Append("<option value='").Append(row["TId"]).Append("'>").Append(row["TName"]).AppendLine("</option>");
??????????????????? }
??????????????????? //獲得html并替換
??????????????????? string htmlStr = CommonHelper.OutputHtml("~/Class/Edit.html");
??????????????????? string html = htmlStr.Replace("actionName","新增").Replace("action","addnew").Replace("@CName","").Replace("@CClassroomNum","").Replace("{teachers}",sb.ToString());
??????????????????? context.Response.Write(html);
??????????????? }
??????????????? else if(action=="edit")
??????????????? {
??????????????????? //獲得id
??????????????????? string idStr=context.Request["id"];
??????????????????? long id = CommonHelper.CheckStringIsLong(idStr);
??????????????????? //獲得所有的老師
??????????????????? DataTable dtTea = SqlHelper.ExecuteQuery("select * from T_Teachers");
??????????????????? StringBuilder sb = new StringBuilder();
??????????????????? foreach(DataRow row in dtTea.Rows)
??????????????????? {
??????????????????????? if(row["TId"].ToString()==idStr)
??????????????????????? {
??????????????????????????? sb.Append("<option selected value='").Append(row["TId"]).Append("'>").Append(row["TName"]).AppendLine("</option>");
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????????? sb.Append("<option value='").Append(row["TId"]).Append("'>").Append(row["TName"]).AppendLine("</option>");
??????????????????????? }
??????????????????? }
??????????????????? //獲得指定id的班級(jí)
??????????????????? DataTable dtCla = SqlHelper.ExecuteQuery("select * from T_Classes where CId=@CId",
??????????????????????? new SqlParameter(){ParameterName="@CId",Value=id});
??????????????????? if(dtCla.Rows.Count<=0)
??????????????????? {
??????????????????????? CommonHelper.OutputErrorHtml("沒有查到該id的數(shù)據(jù):"+id);
??????????????????? }
??????????????????? else if (dtCla.Rows.Count == 1)
??????????????????? {
??????????????????????? DataRow row = dtCla.Rows[0];
??????????????????????? //獲得html并替換
??????????????????????? string htmlStr = CommonHelper.OutputHtml("~/Class/Edit.html");
??????????????????????? string html = htmlStr.Replace("actionName", "編輯").Replace("action", "edit").Replace("@CId", idStr).Replace("@CName", row["CName"].ToString()).Replace("@CClassroomNum", row["CClassroomNum"].ToString()).Replace("{teachers}", sb.ToString());
??????????????????????? context.Response.Write(html);
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? CommonHelper.OutputErrorHtml("存在多個(gè)id的數(shù)據(jù):"+id);
??????????????????? }
?????????????????? ?
??????????????? }
??????????????? else
??????????????? {
??????????????????? CommonHelper.OutputErrorHtml("action錯(cuò)誤:" + action);
??????????????? }
??????????? }
??????????? else if(save=="save") //保存-------------------------------------------------------------------------
??????????? {
??????????????? //獲得共同的項(xiàng)
??????????????? string cname = context.Request["CName"];
??????????????? string cclassroomNum = context.Request["CClassroomNum"];
??????????????? string tidStr = context.Request["TId"];
??????????????? long tid = CommonHelper.CheckStringIsLong(tidStr);
??????????????? //獲得共同的參數(shù)
??????????????? List<SqlParameter> list = new List<SqlParameter>();
??????????????? SqlParameter[] param = {
?????????????????????????????????????????? new SqlParameter("@CName",cname),
?????????????????????????????????????????? new SqlParameter("@CClassroomNum",cclassroomNum),
?????????????????????????????????????????? new SqlParameter(){ParameterName="@TId",Value=tid}
?????????????????????????????????????? };
??????????????? list.AddRange(param);
??????????????? //判斷新增還是編輯
??????????????? int i = -1;
??????????????? if (action == "addnew")
??????????????? {
???????????????????? i = SqlHelper.ExecuteNonQuery("insert into T_Classes(CName,CClassroomNum,TId) values(@CName,@CClassroomNum,@TId)", param);
??????????????? }
??????????????? else if (action == "edit")
??????????????? {
??????????????????? //huode id
??????????????????? string cidStr = context.Request["CId"];
??????????????????? long cid = CommonHelper.CheckStringIsLong(cidStr);
??????????????????? list.Add(new SqlParameter() { ParameterName = "@CId", Value = cid });
??????????????????? i = SqlHelper.ExecuteNonQuery("update T_Classes set CName=@CName,CClassroomNum=@CClassroomNum,TId=@TId where CId=@CId", list.ToArray());
??????????????? }
??????????????? else
??????????????? {
??????????????????? CommonHelper.OutputErrorHtml("action錯(cuò)誤:" + action);
??????????????? }
??????????????? if(i>0)
??????????????? {
??????????????????? context.Response.Redirect("~/Class/List.ashx");
??????????????? }
??????????????? else
??????????????? {
??????????????????? CommonHelper.OutputErrorHtml("操作失敗");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? CommonHelper.OutputErrorHtml("save錯(cuò)誤:" + save);
??????????? }
??????? }
?? ?//班級(jí)---Delete
?? ?public void ProcessRequest(HttpContext context)
??????? {
??????????? context.Response.ContentType = "text/html";
??????????? string cidStr = context.Request["id"];
??????????? long cid = CommonHelper.CheckStringIsLong(cidStr);
??????????? //刪除指定cid的班級(jí),需要先刪除指定cid的學(xué)生
??????????? int count = (int)SqlHelper.ExecuteScalar("select COUNT(*) from T_Students where CId=@CId",
??????????????? new SqlParameter() { ParameterName = "@CId", Value = cid });
??????????? int i = -1;
??????????? if(count>0) //有該班級(jí)的學(xué)生
??????????? {
??????????????? i = SqlHelper.ExecuteNonQuery("delete from T_Students where CId=@CId",
??????????????????? new SqlParameter() { ParameterName = "@CId", Value = cid });
??????????? }
??????????? i = SqlHelper.ExecuteNonQuery("delete from T_Classes where CId=@CId",
??????????????? new SqlParameter() { ParameterName = "@CId", Value = cid });
??????????? if(i>0)
??????????? {
??????????????? context.Response.Redirect("~/Class/List.ashx");
??????????? }
??????????? else
??????????? {
??????????????? CommonHelper.OutputErrorHtml("刪除失敗");
??????????? }
??????? }
28節(jié)
NET代碼運(yùn)行在服務(wù)器JS運(yùn)行在客服端
//1
alert('哈哈'); //是阻塞執(zhí)行,只有窗口關(guān)閉才會(huì)執(zhí)行后面的代碼
//2
alert('刪除成功'); //輸出給瀏覽器是js代碼,但是對(duì)服務(wù)器就是一段字符串,alert是運(yùn)行在瀏覽器端得,所以不會(huì)等用戶關(guān)閉alert之后才向后執(zhí)行
context.Response.Redirect('sefe1.html');
//3
MessageBox.Show("刪除成功"); //可以阻塞C#運(yùn)行,但是這個(gè)對(duì)話框是彈在服務(wù)器的,用戶右看不到
context.Response.Redirect('errorMsg.html');
//4?? ?正確的實(shí)現(xiàn)
response.write("<script type='text/javascript'>alert('刪除成功');location href='errorMsg.html';</script>")
File.WriteAllBytes("d:\\1.exe",new byte[]{3,5,6,7,5543}); //用戶訪問時(shí),寫的.exe病毒也是寫在服務(wù)器端的
29節(jié)
網(wǎng)站安全(不要相信瀏覽器)
//取款
因?yàn)橛脩艨梢酝ㄟ^各種手段偽造http請(qǐng)求,服務(wù)器必須做教驗(yàn)
如果只是把id藏起來,依然不安全,因?yàn)榭梢酝ㄟ^地址拿到數(shù)據(jù)
//IP地址不可以造假,但是可以通過代碼服務(wù)器來轉(zhuǎn)發(fā),服務(wù)器拿到的就是代理服務(wù)器的IP
//投票網(wǎng)站如何避免刷票 (不用用IP,互聯(lián)網(wǎng)中也無法獲得用戶的MAC地址,使用驗(yàn)證碼、手機(jī)號(hào))
//-------------------Day4-----------------
四章?? ?
30節(jié)
文本自增演示HttpHandler不記狀態(tài)
//復(fù)制一個(gè).ashx需要改類名,再改文件中的class名
瀏覽器記不住上次提交的值,下次再提交給服務(wù)器相當(dāng)于重新再來
每次請(qǐng)求都會(huì)重新new一個(gè)新的對(duì)象HttpHandler
HttpHandler記憶--隱藏字段hidden,Cookie,Session
//1?? ?hidden:html的hidden記憶ashx的值并表單提交給HttpHandler
??????????? context.Response.ContentType = "text/html";
??????????? string save = context.Request["save"];
??????????? string htmlStr = CommonHelper.OutputHtml("~/IncAddself2.html");
??????????? if(string.IsNullOrEmpty(save)) //第一次進(jìn)入
??????????? {
??????????????? string html = htmlStr.Replace("@number", "0");
??????????????? context.Response.Write(html);
??????????? }
??????????? else //提交進(jìn)入
??????????? {
??????????????? int number =Convert.ToInt32( context.Request["number"]);
??????????????? number++;
??????????????? string html = htmlStr.Replace("@number", number.ToString());
??????????????? context.Response.Write(html);
??????????? }
??? <form action="IncAddSelf2.ashx" method="post">
??????? @number
??????? <input type="text" name="number" value="@number" />
??????? <input type="submit" name="save" style="width:@number0px" value="提交" />
??? </form>
31節(jié)
Cookie入門
每次向服務(wù)器請(qǐng)求,除了表單信息,還需把和站點(diǎn)有關(guān)的(服務(wù)器給它的)所有Cookie都提交給服務(wù)器
在服務(wù)器把cookie寫入瀏覽器
//CookieTest1.ashx
HttpCookie cookie=new HttpCookie("test");
cookie.Value="rupeng.com"; //Cookie:鍵值對(duì)
context.Response.SetCookie(cookie);
讀取Cookie
//CookieTest2.ashx
HttpCookie cookie1 = context.Request["test"];
context.Response.Write(cookie==null?"沒有cookie":cookie.Value); //cookie的值發(fā)生改變,下次響應(yīng)才會(huì)改變
//2?? ?Cookie:上次在服務(wù)器設(shè)置的Cookie,下次可以直接讀取和更改該Key的Cookie值
?? ?//CookieTest1.ashx
??????????? HttpCookie cookie = new HttpCookie("test");
??????????? cookie.Value = "rupeng.com";
??????????? context.Response.SetCookie(cookie);
?? ?//CookieTest2.ashx
??????????? HttpCookie cookie1 = context.Request.Cookies["test"];
??????????? context.Response.Write(cookie1 == null ? "沒有該key的Cookie" : cookie1.Value);
??????????? cookie1.Value = "iloveyou"; //更改cookie的值
??????????? context.Response.SetCookie(cookie1.Value);
32節(jié)
Cookie的細(xì)節(jié)問題
cookie的有效時(shí)間是程序運(yùn)行期間
cookie.Expires=DateTime.Now.AddSeconds(20); //在DateTime的基礎(chǔ)上增加20秒,返回新的DateTIme對(duì)象
如果不設(shè)定Expires超時(shí)時(shí)間,則關(guān)閉瀏覽器Cookie失效
如果設(shè)定Expires超時(shí)時(shí)間,,除非到期,否則下次訪問瀏覽器Cookie依然存在
不同瀏覽器的Cookie是不能共享的
cookie.Value=context.Request.UserAgent; //把UserAgent存入Cookie
瀏覽器的隱身模式、小號(hào)模式,所有的Cookie信息都與主瀏覽器不一致,達(dá)到隱身的效果
Cookie的大小是有限的,不能存儲(chǔ)過多的信息
機(jī)密信息不能存入Cookie
Cookie是可以被清除的,也許沒到Expires就已經(jīng)過期了
33節(jié)
Cookie實(shí)現(xiàn)記住用戶名
用戶第一次進(jìn)入首先看看Cookie中有沒有值
//直接進(jìn)入時(shí),從Cookie中獲得用戶名
Http cookieLastUserName = context.Request.Cookies["LastUserName"];
//登陸進(jìn)入后,把用戶名設(shè)置到Cookie中
HttpCookie cookieUserName = new HttpCookie("LastUserName");
cookieUserName.Value = username;
cookieUserName.Expires = DateTime.Now.AddDays(7);
context.Response.SetCookie(cookieUserName);
34節(jié)
Cookie的深入(*)
//1
關(guān)于Cookie的Path(路徑)問題
cookie.Path=null;
cookie.Path="/Day4/CookiePath1.ashx"; //Path:誰能讀我
如果沒有設(shè)定Path,則Path的默認(rèn)值是 "/" ,即當(dāng)前域名下所有的頁面都可以操作這個(gè)Cookie
//2
不同域名下的Cookie不相干,但是一個(gè)主域名下的兩個(gè)子域名通過設(shè)置Cookie.Domain是可以互相操作(共用)的
www.rupeng.comm
pps.rupeng.comm //這個(gè)2個(gè)域名為同一個(gè)主域名repeng.com下的子域名,他們默認(rèn)的Cookie也不能互相操作;但是設(shè)置Cookie.Domain='.rupeng.com',則他們的子域名都可以操作這個(gè)Cookie
35節(jié)
Session(會(huì)話)入門
服務(wù)器端的Cookie,無法造假
給客戶一個(gè)“身份證”,可以唯一標(biāo)識(shí)這個(gè)用戶,核心信息放到服務(wù)器上,客戶端無法去篡改
ASP.net內(nèi)置了Session機(jī)制,.ashx要操作Session必須實(shí)現(xiàn)IRequiresSessionState接口,不實(shí)現(xiàn)的話Session會(huì)為null
這個(gè)接口沒有任何方法,為標(biāo)記接口,因?yàn)镾ession處理會(huì)稍微降低系統(tǒng)性能,所以這種HttpHandler默認(rèn)不處理Session,如果實(shí)現(xiàn)接口才處理
context.Session["test2"]=888; //cookie只能寫string,而session可以是任意類型
int i1=(int)context.Session["test2"]; //錯(cuò)誤,因?yàn)槿f一session中為Null,應(yīng)該int?
Session依賴于Cookie,所在在別的瀏覽器沒有,關(guān)閉后也沒有了
Cookie存在瀏覽器中,Session存在服務(wù)器中,當(dāng)前網(wǎng)站的任何一個(gè)頁面都可能取到Session
服務(wù)器會(huì)定期銷毀服務(wù)器端的Session
Session的失效時(shí)間是在Web.Config中設(shè)置的<SessionState timeout="20"> 設(shè)置超時(shí)時(shí)間,默認(rèn)時(shí)20minutes,也許服務(wù)器壓力大10minutes就失效了
//Session的保存
context.Session["test1"] = "rupeng.com";
context.Session["test2"] = 888;
//Session的獲取
string test1 = (string)context.Session["test1"];
int? test2 = (int?)context.Session["test2"];
context.Response.Write(test1 + "," + test2);
//只有admin這個(gè)用戶才能訪問頁面1、頁面2,其他用戶沒有權(quán)限訪問
//Login1.ashx
??? if (string.IsNullOrEmpty(context.Request["login"])) //直接進(jìn)入
??? {
??????? string username = (string)context.Session["username"];
??????? context.Response.Write(username == null ? "請(qǐng)先登陸" : "username=" + username + ",登陸成功!");
??? }
??? else //登陸進(jìn)入
??? {
??????? string username = context.Request["username"];
??????? string password = context.Request["password"];
??????? if (password == "123")
??????? {
??????????? //登陸成功
??????????? context.Session["username"] = username;
??????????? context.Response.Write("登陸成功"+username);
??????? }
??????? else
??????? {
??????????? context.Response.Write("登陸失敗");
??????? }
??? }
//Page1.ashx
??? string username = (string)context.Session["username"];
??? context.Response.Write(username == "admin" ? "登陸成功" : "username=" + username + ",沒有權(quán)限進(jìn)入該頁面!");
36節(jié)
Session案例:登陸后返回到之前頁面
登陸,修改密碼,查詢余額
//移除google上的自動(dòng)填充表單內(nèi)容
//最好把key這些不變值定義為常量,避免寫錯(cuò)
public const string LOGINUSERNAME;
web.config中timeout設(shè)置session的有效時(shí)間
如果訪問某個(gè)頁面進(jìn)行登陸,登陸之后返回之前的頁面(從哪個(gè)頁面來,登陸后,返回原來頁面)
context.Session[Login.LOGNURL]=context.Request.Url.ToString(); //把當(dāng)前地址存到session
關(guān)閉瀏覽器,服務(wù)器的Session還會(huì)存在一段時(shí)間,而Abandon是立即銷毀
context.Session.Abandon(); //銷毀
//Login.ashx
public const string LOGINUSERNAME = "loginUserName";
public const string LOGINURL = "loginUrl"; //記錄登陸前,進(jìn)入的頁面
public const string YZM = "yanzhengCode";
public void ProcessRequest(HttpContext context)
{
??? context.Response.ContentType = "text/html";
??? string html = CommonHelper.OutputHtml("~/Login/Login.html");
??? if (string.IsNullOrEmpty(context.Request["subLogin"])) //直接進(jìn)入
??? {
??????? string lastusername = (string)context.Session[LOGINUSERNAME];
??????? if (lastusername == null) //session中沒有登陸信息
??????? {
??????????? html = html.Replace("@msg", "");
??????????? context.Response.Write(html);
??????? }
??????? else //session中有值
??????? {
??????????? context.Response.Redirect("ExchangePassword.ashx");
??????? }
??? }
??? else //登陸進(jìn)入
??? {
??????? string username = context.Request["username"];
??????? string password = context.Request["password"];
?? ?string yzm = context.Request["yanzhengCode"];
??????? //先判斷驗(yàn)證碼
??????? string yzmInServer = context.Session[YZM].ToString(); //服務(wù)器中的驗(yàn)證碼
??????? if(yzm!=yzmInServer)
??????? {
??????????? html = html.Replace("@msg", "驗(yàn)證碼錯(cuò)誤");
??????????? context.Response.Write(html);
??????????? return;
??????? }
??????? //判斷用戶名與密碼??? ?
??????? int i = (int)SqlHelper.ExecuteScalar("select COUNT(*) from T_UserInfoes where UserName=@UserName and Password=@Passwo
??????????? new SqlParameter("@UserName",username),
??????????? new SqlParameter("@Password",password));
??????? if(i<=0)
??????? {
??????????? html = html.Replace("@msg","用戶名或密碼錯(cuò)誤");
??????????? context.Response.Write(html);
??????? }
??????? else if(i==1) //登陸成功
??????? {
??????????? context.Session[LOGINUSERNAME] = username;
??????????? string loginUrl = (string)context.Session[LOGINURL];
??????????? //從哪個(gè)界面進(jìn)入,就跳轉(zhuǎn)到哪個(gè)界面
??????????? if (loginUrl == null) //沒有最先的跳轉(zhuǎn)頁面
??????????? {
??????????????? context.Response.Redirect("ExchangePassword.ashx");
??????????? }
??????????? else
??????????? {
??????????????? context.Response.Redirect(loginUrl);
??????????? }
??????? }
??????? else
??????? {
??????????? html = html.Replace("@msg", "服務(wù)器錯(cuò)誤,存在相同用戶名");
??????????? context.Response.Write(html);
??????? }
??? }
//ExchangePasword.ashx,QueryMoney.ashx
??? context.Response.ContentType = "text/plain";
??? string lastusername = (string)context.Session[Login.LOGINUSERNAME];
??? if (lastusername == null) //Session中沒有登陸信息
??? {
??????? context.Session[Login.LOGINURL] = context.Request.Url.ToString();
??????? context.Response.Redirect("Login.ashx");
??? }
??? else
??? {
??????? context.Response.Write("修改" + lastusername + "的密碼.....<a href='EndLogin.ashx'>退出(銷毀Session,取消當(dāng)前會(huì)話)</a>" );
??? }
EndLogin.ashx
??? context.Session.Abandon(); //銷毀Session,取消當(dāng)前會(huì)話
??? context.Response.Redirect("Login.ashx");
//37節(jié)
Session案例:驗(yàn)證碼
如果沒有驗(yàn)證碼,可以通過不斷試驗(yàn)密碼進(jìn)行暴力破解
驗(yàn)證碼是用來區(qū)分發(fā)出請(qǐng)求信息的是人還是計(jì)算機(jī),因?yàn)閺?fù)雜的圖片計(jì)算機(jī)很難識(shí)別,而人卻可以比較輕松的識(shí)別出來的
正確的驗(yàn)證碼放在服務(wù)器的Session中,用戶名輸入的與之進(jìn)行比對(duì)
//首先需要生成一張驗(yàn)證碼圖片(復(fù)雜圖片需要很多計(jì)算機(jī)圖形學(xué)的理論基礎(chǔ))
刷新驗(yàn)證碼圖片時(shí),如果還是原來的路徑Y(jié)anZhengMa.ashx,因?yàn)槁窂奖痪彺媪?所以不會(huì)再刷新,只有路徑后+new Date();讓這次的src路徑與上次不一樣,才會(huì)重新創(chuàng)建一個(gè)隨機(jī)數(shù),刷新驗(yàn)證碼
//然后驗(yàn)證用戶名輸入驗(yàn)證碼是否正確
//generateYanZhengCode.ashx
??? context.Response.ContentType = "image/jpeg";
??? //產(chǎn)生隨機(jī)數(shù)
??? Random ran = new Random();
??? int num = ran.Next(1000, 10000);
??? context.Session[Login.YZM] = num; //把驗(yàn)證碼存入會(huì)話
??? //new一個(gè)圖片,在畫布上畫上隨機(jī)數(shù)
??? using (Bitmap bmp = new Bitmap(40, 20))
??? {
??????? using (Graphics g = Graphics.FromImage(bmp))
??????? using(Font font=new Font(FontFamily.GenericSansSerif,10))
??????? {
??????????? g.DrawString(num.ToString(), font, Brushes.Red, new PointF(0, 0));
??????? }
??????? bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
??? }
//Login.ashx
??? function refreshCode() {
??????? var im = document.getElementById("im");
??????? im.src = "generateYanZhengCode.ashx?" + new Date();
??? };
??? <td>驗(yàn)證碼</td><td><img src="generateYanZhengCode.ashx" id="im" οnclick="refreshCode();" /><input type="text" style="width:60px;" name="yanzhengCode" /></td>
38節(jié)
Session的原理
在一個(gè)請(qǐng)求期間,為同一個(gè)SessionId
Guid算法:Guid guid=Guid.NewGuid();
Guid用于產(chǎn)生全球唯一的字符串,是根據(jù)網(wǎng)卡mac地址、系統(tǒng)時(shí)間、CPU時(shí)鐘周期,在同一臺(tái)多次調(diào)用不會(huì)重復(fù)、在不同電腦同一時(shí)間也不會(huì)重復(fù)
瀏覽器放了一個(gè)SessionId,服務(wù)器保存了SessionId的值的對(duì)應(yīng)關(guān)系
創(chuàng)建一個(gè)SessionId,并設(shè)置到Cookie中;保存時(shí),把Value保存到指定SessionId的文件中;取值時(shí),從指定SessionId的文件中取Value-----------------(*)
//原理:瀏覽器請(qǐng)求服務(wù)器,判斷Cookie中有沒有"SessionId"這樣一個(gè)Cookie的值,如果Cookie中有個(gè)SessionId,如果查到SessionId的值,則在磁盤中找這個(gè)文件,如果沒有這個(gè)文件,則創(chuàng)建一個(gè)文件,這個(gè)文件就用來保存這個(gè)Session的相關(guān)信息;如果沒有這個(gè)SessionId,則創(chuàng)建一個(gè)Guid.NewGuid(),然后返回給瀏覽器.總之,服務(wù)器中有放SessionId的文件,保存Session的數(shù)據(jù)庫,瀏覽器通過這個(gè)文件的SessionId來向服務(wù)器請(qǐng)求Session數(shù)據(jù).
//1CreateSession():創(chuàng)建一個(gè)SessionId,設(shè)置到Cookie中
//2構(gòu)造函數(shù):
//如果SessionId不存在,就創(chuàng)建,否則獲得這個(gè)SessionId,以局部變量存起來
//判斷請(qǐng)求中是否有這個(gè)SessionId
HttpCookie cookie=context.Request.Cookies[RUPENGSESSIONID];
return cookie!=null; //有SessionId
//3SetValue():把SessionId寫入文件(把context用局部變量存起來,方便每次使用)
File.WriteAllText(fullPath,value);
//4GetValue()
File.ReadAllText(fullPath);
//Session原理:RupengSesion.cs
public class RupengSession
{
??? private const string RUPENGSESSIONID = "RupengSessionId";
??? private string sessionId;
??? private HttpContext context;
??? public RupengSession(HttpContext context)
??? {
??????? this.context = context;
??????? //判斷Cookie中有沒有RupengSessionId
??????? HttpCookie cookie = context.Request.Cookies[RUPENGSESSIONID];
??????? if (cookie == null)
??????? {
??????????? CreateSession();
??????? }
??????? else
??????? {
??????????? this.sessionId = cookie.Value;
??????? }
??? }
??? //創(chuàng)建Session
??? private void CreateSession()
??? {
??????? Guid guid = Guid.NewGuid();
??????? this.sessionId = guid.ToString();
??????? HttpCookie cookie = new HttpCookie(RUPENGSESSIONID);
??????? cookie.Value = sessionId;
??????? context.Response.SetCookie(cookie);
??? }
??? //設(shè)值(存入MySession的值)
??? public void SetValue(string value)
??? {
??????? string fullPath = context.Server.MapPath("~/MySession/SessionId/" + this.sessionId);
??????? File.WriteAllText(fullPath, value);
??? }
??? //取值
??? public string GetValue()
??? {
??????? string fullPath = context.Server.MapPath("~/MySession/SessionId/" + this.sessionId);
??????? if(!File.Exists(fullPath))
??????? {
??????????? return null;
??????? }
??????? return File.ReadAllText(fullPath);
??? }
//登錄(寫入Sessioin):Login1.ashx
RupengSession rupengSession = new RupengSession(context); //把SessionId設(shè)置到了Cookie中
rupengSession.SetValue(username); //把username存入SessionId所對(duì)應(yīng)的文件中
context.Response.Redirect("MainPage.ashx");
//讀取Session:MainPage.ashx
RupengSession rupengSession = new RupengSession(context);
string username = rupengSession.GetValue();
if (username != null)
{
??? context.Response.Write("您當(dāng)前登錄用戶名為:" + username);
}
else
{
??? context.Response.Write("未登錄");
}
39節(jié)
改造RupengSession
怎么樣使Session放入多個(gè)值:對(duì)象序列化
序列化:把內(nèi)存中對(duì)象保存為二進(jìn)制數(shù)據(jù)
被序列化的對(duì)象必須標(biāo)上[Serializable]
//1序列化對(duì)象:把對(duì)象保存到流中
Person per=new Person();
BinaryFormatter bf=new BinaryFormatter();
using(Stream stream=File.OpenWrite("d:\\1.bin"))
{
?? ?bf.Serialize(stream,per); //序列化指定對(duì)象到指定stream中
}
//2反序列化:從流中讀取出來
BinaryFormatter bf=new BinaryFormatter();
using(Stream stream=File.OpenRead("d:\\1.bin"))
{
?? ?Person per=(Person)df.Deserialize(stream);
}
//SetValue(string name,string value)設(shè)值時(shí),如果文件存在,就反序列化文件中的內(nèi)容,放入dict中;如果不存在,創(chuàng)建一個(gè)空的dict;添加一個(gè)dict,再把dict序列化到指定SessionId文件中
//GetValue(string name)如果文件存在,反序列化為dict,獲得指定dict[name];如果文件不存在,返回null
//MySerializeSession.RupengSession.cs
//設(shè)值:把這個(gè)值保存到指定sessionId的文件中
public void SetValue(string name, string value)
{
??? Dictionary<string, string> dict = new Dictionary<string, string>();
??? string path = context.Server.MapPath("~/MySerializeSession/SessionId/" + this.sessionId);
??? if (File.Exists(path)) //如果文件存在,就從指定文件流中反序列化出其中內(nèi)容,加入新的值后,再重新序列化到流文件中
??? {
??????? dict = DeserializeFormStream(path);
??? }
??? dict[name] = value;
??? //再重新序列化到流文件中
??? using(Stream stream=File.OpenWrite(path))
??? {
??????? BinaryFormatter bf = new BinaryFormatter();
??????? bf.Serialize(stream, dict); //把指定對(duì)象序列化到指定流中
??? }
}
//取值:從指定SessionId的文件中獲得資格值
public string GetValue(string name)
{
??? string path = context.Server.MapPath("~/MySerializeSession/SessionId/" + this.sessionId);
??? if(!File.Exists(path))
??? {
??????? return null;
??? }
??? //如果文件存在
??? Dictionary<string, string> dict = new Dictionary<string, string>();
??? dict = DeserializeFormStream(path);
??? return dict[name];
}
//從流中讀取文件并反序列化為對(duì)象
private Dictionary<string,string> DeserializeFormStream(string path)
{
??? using (Stream stream = File.OpenRead(path))
??? {
??????? BinaryFormatter bf = new BinaryFormatter();
??????? return (Dictionary<string, string>)bf.Deserialize(stream);
??? }
}
//Login1.ashx
RupengSession rupengSession = new RupengSession(context); //把SessionId設(shè)置到了Cookie中
rupengSession.SetValue("username", username); //把username存入SessionId所對(duì)應(yīng)的文件中
rupengSession.SetValue("lastLoginTime", DateTime.Now.ToString());
//MainPage.ashx
RupengSession rupengSession = new RupengSession(context);
string username = rupengSession.GetValue("username");
if (username != null)
{
??? context.Response.Write("您當(dāng)前登錄用戶名為:" + username);
??? context.Response.Write("您當(dāng)前登錄時(shí)間為:" + rupengSession.GetValue("lastLoginTime"));
}
else
{
??? context.Response.Write("未登錄");
}
39節(jié)
進(jìn)程外Session
Asp.net中的Session默認(rèn)保存在Web服務(wù)器內(nèi)存中的(Inprov),Web服務(wù)器重啟,所有Session數(shù)據(jù)都會(huì)消失
session.web中sessionState中的timeout="10"
避免Session重啟后就消失:把Session存在別的地方,如本次的練習(xí),以及把Session存到進(jìn)程外SqlServer和sessionState
//1 Session保存到SQLServer中配置方法
Windows--NET---4.0.30319--aspnet_regsql.exe--ssadd···(哪個(gè)數(shù)據(jù)庫:ip,db,uid,pwd;參數(shù)見http://www.cnblogs.com/China-Dragon/archive/2009/05/12/1455147.html)
然后修改web.config中sessionState節(jié)點(diǎn)的配置:<sessionState mode="SQLServer" timeout="20" sqlConnectionString="server=.;uid=sa;password=123456;"></sessionState>
//2 Session保存到Windows服務(wù)中的StateServer中---怎么配置自己到百度搜
網(wǎng)絡(luò)項(xiàng)目最好用這種進(jìn)程外Session
http://www.cnblogs.com/China-Dragon/archive/2009/05/12/1455147.html ----------------------------???---------------------
-E是使用Windows認(rèn)證,也可以使用數(shù)據(jù)庫認(rèn)證
aspnet_regsql.exe -ssadd -sstype c -d [DB]? -S [Server] –U [User Name] – P [Password]
2. 修改web.config:
<sessionState mode="SQLServer" allowCustomSqlDatabase="true" sqlConnectionString="data source=[Server];initial catalog=[DB];user id=[User Name];password=[Password]"
??????????????? cookieless="false"
??????????????? timeout="20" />
如果使用默認(rèn)的數(shù)據(jù)庫名稱,如下:
<sessionState mode="SQLServer" sqlConnectionString="data source=[Server];user id=[User Name];password=[Password]"
??????????????? cookieless="false"
??????????????? timeout="20" />
//------------------------------------
40節(jié)
轉(zhuǎn)載于:https://www.cnblogs.com/adolphyang/p/4766770.html
總結(jié)
以上是生活随笔為你收集整理的Asp.Net Core(.net内核)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java的4种代码块
- 下一篇: 用DOM解析XML