html5与asp.net程序...,asp.net 一般处理程序(5)-(C#)

asp.net 一般处理程序(5)-(C#)

发布时间:2020-10-01 12:55:17

来源:51CTO

阅读:1044

作者:海莉zhe

一般处理程序:其实它本质上就是一个类,但是它需要注意几个方面:

ffe55116e41aac5d5fa06475240b874b.png

(1)需要实现一个IHttpHandler的接口,这是因为它在asp.net的运行原理中,在创建被请求的页面类时,需要把它转成接口,然后再实现接口里面的Proce***equest()方法;

(2)里面还需要实现IsReusable() 的方法,它是表示在服务器上是否可以重用(设置为true 即为可重用,一般默认设置为false)

同时我还简单利用一般处理程序,写了一个简单的计算器,希望和大家一同深入体会一下一般处理程序的运用。

首先,我是利用Html[作为前台]      +     一般处理程序(.ashx)[业务代码]:

03cd7e881eea766e4581c8a83b901925.png

C02index.html代码:

+

-

*

/

=

C02index.ashx 一般处理程序的代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace Calulate

{

///

/// c02index 的摘要说明

///

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方式发过来的参数

string strNum1=context.Request.Form["txtNum1"];

string strNum2=context.Request.Form["txtNum2"];

//4.定义返回的变量

int x=0, y=0, z=0;

//5.判断接收的参数

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("除数不能为零");

}

}

}

}

//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;

}

}

}

}

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

695d323c6af39090e727504d66ee07b2.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值