一般處理程序:其實它本質(zhì)上就是一個類,但是它需要注意幾個方面:
(1)需要實現(xiàn)一個IHttpHandler的接口,這是因為它在asp.net的運行原理中,在創(chuàng)建被請求的頁面類時,需要把它轉(zhuǎn)成接口,然后再實現(xiàn)接口里面的Proce***equest()方法;
(2)里面還需要實現(xiàn)IsReusable() 的方法,它是表示在服務(wù)器上是否可以重用(設(shè)置為true 即為可重用,一般默認設(shè)置為false)
同時我還簡單利用一般處理程序,寫了一個簡單的計算器,希望和大家一同深入體會一下一般處理程序的運用。
首先,我是利用Html[作為前臺] + 一般處理程序(.ashx)[業(yè)務(wù)代碼]:

C02index.html代碼:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action="c02index.ashx" method="post">
<input type="hidden" name="hidIsPostBack" value="1"/>
<input type="text" name="txtNum1" value="{num1}"/><select id="Select1" name="Sel">
<option selected="selected">+</option>
<option selected="selected">-</option>
<option selected="selected">*</option>
<option selected="selected">/</option>
</select>
<input type="text" name="txtNum2" value="{num2}"/>=
<input type="text" name="txtSum" value="{res}"/><br />
<input type="submit" value ="計算"/>
</form>
</body>
</html>C02index.ashx 一般處理程序的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Calulate
{
/// <summary>
/// c02index 的摘要說明
/// </summary>
public class c02index : IHttpHandler
{
public void Proce***equest(HttpContext context)
{
context.Response.ContentType = "text/html";
//1.通過虛擬路徑 獲取絕對路徑
string PhyPath = context.Server.MapPath("c02index.html");
//2.通過絕對路徑獲取文件值
string strHtml = System.IO.File.ReadAllText(PhyPath);
//3.獲取瀏覽器的post方式發(fā)過來的參數(shù)
string strNum1=context.Request.Form["txtNum1"];
string strNum2=context.Request.Form["txtNum2"];
//4.定義返回的變量
int x=0, y=0, z=0;
//5.判斷接收的參數(shù)
if (!string.IsNullOrEmpty(context.Request.Form["hidIsPostBack"]))
{
if(!string.IsNullOrEmpty(strNum1) &&!string.IsNullOrEmpty(strNum2))
{
if(int.TryParse(strNum1,out x) && int.TryParse(strNum2,out y))
{
if (context.Request.Form["Sel"] == "+")
{
z = x + y;
}
else if (context.Request.Form["Sel"] == "-")
{
z = x - y;
}
else if (context.Request.Form["Sel"] == "*")
{
z = x * y;
}
else if (context.Request.Form["Sel"] == "/")
{
if (y != 0)
{
z = x / y;
}
else
{
throw new Exception("除數(shù)不能為零");
}
}
}
}
//6.替代字符串 并接收替代后的返回值
strHtml = strHtml.Replace("{num1}", x.ToString()).Replace("{num2}", y.ToString()).Replace("{res}", z.ToString());
//7.把字符串返回給瀏覽器
context.Response.Write(strHtml);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}











另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標題名稱:asp.net一般處理程序(5)-(C#)-創(chuàng)新互聯(lián)
文章路徑:http://chinadenli.net/article4/ccgooe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站制作、外貿(mào)建站、企業(yè)網(wǎng)站制作、手機網(wǎng)站建設(shè)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容