在前两篇文章中,我们已经创建了一个HTTP处理程序,并实现了页面到HTTP处理程序之间的字符串参数的传递。下面让我们来作个比较实用的东西--一个类似于 Google Analytics 的GridView
在前两篇文章中,我们已经创建了一个HTTP处理程序,并实现了页面到HTTP处理程序之间的字符串参数的传递。下面让我们来作个比较实用的东西--一个类似于 Google Analytics 的GridView,实现后的效果见下图。
实现方法
Step1:实现动态生成图片。首先,为类库 mylib.system.web 添加对 System.Drawing 的引用。然后,编写如下代码根据传给 MyHandler.jxd 的参数动态生成一个图片,并写到 Response 的 OutputsStream 里面。MyHandler.cs的代码如下

MyHandler.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace mylib.system.web
{
public class MyHandler : System.Web.IHttpHandler
{
#region IHttpHandler 成员
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(System.Web.HttpContext context)
{
// 获取参数n,并将其转换为Int型。
string n = context.Request.QueryString["n"];
int count = 0;
int.TryParse(n, out count);
// 使用GDI+函数画一个长方形。
Bitmap bmp = new Bitmap(count, 20);
Graphics graphic = Graphics.FromImage(bmp);
MemoryStream stream = new MemoryStream();
SolidBrush brush = new SolidBrush(Color.SteelBlue);
try
{
graphic.FillRectangle(brush, 0, 0, count, 20);
// 将画好的图形保存到内存中。
bmp.Save(stream, ImageFormat.Png);
// 将内存中的图片发送到客户端
context.Response.ContentType = "image/png";
context.Response.OutputStream.Write(stream.ToArray(),0,(int)stream.Length);
}
finally
{
// 释放资源
stream.Close();
brush.Dispose();
brush = null;
graphic.Dispose();
graphic = null;
}
}
#endregion
}
}
Step2:在Web程序的 Default.aspx 中放置一个 GridView 控件。并为其添加两个模板列。一列用于显示“网址”;另一列中放一个 Image 控件和一个 Label 控件,用于显示访问量。Label 控件直接绑定到数组下标为1的字段,就不用多说了。Image 控件通过“ImageUrl= '<%# "~/MyHandler.jxd?n=" + Eval("[1]") %>'”这样的绑定表达式向 MyHandler.jxd 发送带参数的请求,Myhandler.cs 将根据参数 n 的值生成相应大小的图片并发送给 Image 控件。

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<Columns>
<asp:TemplateField HeaderText="网址">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[0]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="访问量">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl= '<%# "~/MyHandler.jxd?n=" + Eval("[1]") %>' />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[1]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
</div>
</form>
</body>
</html>
Step3:作一些测试数据,并绑定到 GridView 中。

Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<string[]> data = new List<string[]>();
data.Add(new string[] { "1. 使用分治法实现的全排列算法", "50" });
data.Add(new string[] { "2. 让Ruby的数组支持任意起始下标", "16" });
data.Add(new string[] { "3. 输出二叉树的方法", "14" });
data.Add(new string[] { "4. 让ruby以矩阵的样式输出二维数组", "9" });
data.Add(new string[] { "5. 算法分析涉及到的一些函数图像", "8" });
GridView1.DataSource = data;
GridView1.DataBind();
}
}
下载本篇全部源代码
本篇到此结束,下一篇将介绍让Web程序与HTTP处理程序共享Session的方法。
本系列共6篇文章
实战 HTTP 处理程序(HTTP Handler) (6)——条码随意打
实战 HTTP 处理程序(HTTP Handler) (5)——不用临时文件,直接打开动态生成的文件
实战 HTTP 处理程序(HTTP Handler) (4)——与Web程序共享Session
实战 HTTP 处理程序(HTTP Handler) (3)——动态生成图片 <- you are here.
实战 HTTP 处理程序(HTTP Handler) (2)——向HTTP 处理程序传递参数
实战 HTTP 处理程序(HTTP Handler) (1)——创建一个最简单的 HTTP Handler
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!