Asp.Net第一章入门之后台处理程序
生活随笔
收集整理的這篇文章主要介紹了
Asp.Net第一章入门之后台处理程序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Asp.Net
C#-->OOP-->Winform--Asp.Net
1.新建空項(xiàng)目
2.建立html頁(yè)面
login.html
? <form action="handler/LoginHandler.ashx" method="post">賬戶:<input type="text" name="uname" /><br />密碼:<input type="password" name="pwd" /><br /><button type="submit">提交</button></form>3.測(cè)試test.ashx
aspx:Web窗體設(shè)計(jì)頁(yè)面。Web窗體頁(yè)由兩部分組成:視覺元素(html、服務(wù)器控件和靜態(tài)文本)和該頁(yè)的編程邏輯(VS中的設(shè)計(jì)視圖和代碼視圖可分別看到它們對(duì)應(yīng)得文件)。VS將這兩個(gè)組成部分分別存儲(chǔ)在一個(gè)單獨(dú)的文件中。視覺元素在.aspx 文件中創(chuàng)建
ashx:.ashx文件是主要用來寫web handler的。使用.ashx 可以讓你專注于編程而不用管相關(guān)的web技術(shù)。我們熟知的.aspx是要做html控件樹解析的,.aspx包含的所有html實(shí)際上是一個(gè)類,所有的html都是類里面的成員,這個(gè)過程在.ashx是不需要的。ashx必須包含IsReusable屬性(這個(gè)屬性代表是否可復(fù)用,通常為true),而如果要在ashx文件用使用Session必須實(shí)現(xiàn)IRequiresSessionState接口.
3.1 查看源碼,理解HttpRequest、HttpResponse
?using System;using System.Collections.Generic;using System.Linq;using System.Web;?namespace demo01.handler{/// <summary>/// test 的摘要說明/// </summary>public class test : IHttpHandler{?public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";context.Response.Write("Hello World");}?public bool IsReusable{get{return false;}}}}3.2 handler/LoginHandler.ashx
?using System;using System.Collections.Generic;using System.Linq;using System.Web;?namespace demo01.handler{/// <summary>/// LoginHandler 的摘要說明/// </summary>public class LoginHandler : IHttpHandler{?public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/html";//context.Response.Write("Hello World");//我們下面的工作,就是需要通過請(qǐng)求對(duì)象,接受網(wǎng)頁(yè)的數(shù)據(jù)string uname = context.Request.Params["uname"].ToString();//context.Response.Write(uname);string pwd = context.Request.Params["pwd"].ToString();?//下一步需要判斷,判斷如果成功,則顯示一句話,否則顯示一句話 if ("admin".Equals(uname) && "123456".Equals(pwd)){context.Response.Write("<font color='red'>成功登錄!</font>");}else {context.Response.Write("<font color='blue'>登錄失敗!</font>");}}?public bool IsReusable{get{return false;}}}}總結(jié)
以上是生活随笔為你收集整理的Asp.Net第一章入门之后台处理程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#连接MySQL时出现Unable t
- 下一篇: Asp.Net第二章服务器端控件