首页 > Web开发 > 详细

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

时间:2014-02-14 22:45:47      阅读:359      评论:0      收藏:0      [点我收藏+]

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

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

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

 

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

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

bubuko.com,布布扣

        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 ProcessRequest(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;
}
}
}
}

       

 

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

 

本文出自 “数据库之家” 博客,请务必保留此出处http://6588779.blog.51cto.com/6578779/1358944

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

原文:http://6588779.blog.51cto.com/6578779/1358944

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!