因为公司项目的原因,对里面的一些ashx文件不是很明白,以前也从来没接触过。所以自己到网上查了下,然后自己写了一些例子,在此做个记录,也顺便给初学的人提供一些方便。

下面是正文:

对于ashx,网上比较多的说法是用来显示图片。然后我试着做了个,确实太简单了。

1、显示图片

aspx代码:

<img src="Handler/Handler2.ashx" alt="这是ashx生成的" />

ashx代码:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;namespaceWebApplication1.Handler
{/// <summary>///Handler2 的摘要说明/// </summary>public classHandler2 : IHttpHandler{public voidProcessRequest(HttpContext context){context.Response.ContentType= "image/png";//context.Response.Write("Hello World");context.Response.WriteFile("~/Images/1.jpg");}public boolIsReusable{get{return false;}}}
}

图片路径正确就行。当然,图片路径可以从数据库中读取,可以传递参数选择图片。具体的不多说了,因为ashx就是一个处理程序的文件,这里面进行逻辑判断业务操作然后返回给页面。

本来我是想先实现文字输出的,可苦于不知道用什么来呈现文字,即找不到文字显示的途径,只好先实现了图片显示。接下来就是实现文字输出的时刻!

2、文字输出

aspx代码:

<script src="Handler/Handler1.ashx" type="text/javascript"></script>

ashx代码:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.IO;namespaceWebApplication1.Handler
{/// <summary>///Handler1 的摘要说明/// </summary>public classHandler1 : IHttpHandler{public voidProcessRequest(HttpContext context){context.Response.ContentType= "text/plain";//context.Response.Write("alert('hi')");context.Response.Write("document.write('HELLO WORLD!!')");}public boolIsReusable{get{return false;}}}
}

这样既实现了文字输出,aspx代码放在<body>标签里即可,类似与这样:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %><!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>测试ashx</title>
</head>
<body><script src="Handler/Handler1.ashx" type="text/javascript"></script><form id="form1" runat="server"><div></div></form>
</body>
</html>

当然你也可以放在<div>标签中。

因为ashx文件不返回html内容,所以一定要写全。如

context.Response.Write("document.write('HELLO WORLD!!')");

另外还有一种页面呈现方式<还有其他的>,用文件流的方式输出成标准的html格式,然后用iframe来引用到需要呈现的页面。

aspx页面:

    <div><iframe src="Handler/Handler3.ashx"></iframe></div>

ashx页面:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.IO;usingSystem.Web;namespaceWebApplication1.Handler
{/// <summary>///Handler3 的摘要说明/// </summary>public classHandler3 : IHttpHandler{public voidProcessRequest(HttpContext context){//Set up the response settingscontext.Response.ContentType = "text/html";context.Response.Cache.SetCacheability(HttpCacheability.Public);context.Response.BufferOutput= false;Stream stream= null;string html = "<html><body>成功: test of txt.ashx</body></html>";byte[] html2bytes = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(html);stream= newMemoryStream(html2bytes);if (stream == null)stream= new MemoryStream(System.Text.Encoding.ASCII.GetBytes("<html><body>get Nothing!</body></html>"));//Write text stream to the response streamconst int buffersize = 1024 * 16;byte[] buffer = new byte[buffersize];int count = stream.Read(buffer, 0, buffersize);while (count > 0){context.Response.OutputStream.Write(buffer,0, count);count= stream.Read(buffer, 0, buffersize);}}public boolIsReusable{get{return false;}}}
}

在上面这个例子中,<body>中的内容完全可以自己根据需要进行整理,比如从数据库获取。

就不一一介绍了,总之ashx可以让代码更加整洁,在很多时候还是很方便的,比如显示图片的时候。

转载于:https://www.cnblogs.com/ygyxinyu/archive/2013/02/20/2918961.html

关于ashx的基本应用相关推荐

  1. ashx导出dataTable为Excel

    一,datatable导出Excel,用户可以选择路径,方法如下: /// <summary>/// DataTable导出到Excel/// </summary>/// &l ...

  2. 结合ashx来在DataGrid中显示从数据库中读出的图片

    作者:木子  http://blog.csdn.net/derny/ 下面利用ashx文件可以方便实现从数据库中读取图片并显示在datagrid当中 //----------------------- ...

  3. Uploadify jquery+falsh+UploadHandler.ashx

    官方网:http://www.uploadify.com/ 只有PHP版本 对于我们.net的来说是一个遗憾!现在奉献一个c#版本,希望对大家有用. 看代码其实很简单,在做这个之前遇到许多问题,特别是 ...

  4. 【转】结合ashx来在DataGrid中显示从数据库中读出的图片

    下面利用ashx文件可以方便实现从数据库中读取图片并显示在datagrid当中 1.BindImage.aspx <%@ Page language="c#" Codebeh ...

  5. ashx是什么文件,如何创建[转]

    原文地址:http://www.cnblogs.com/lin614/archive/2008/01/18/1044734.html ashx是什么文件,如何创建 .ashx 文件用于写web han ...

  6. ashx文件和HttpHandler

    ashx 文件用于写web handler的..ashx必须包含IsReusable. 如下例所示.}.ashx比.aspx的好处在与不用多一个html 注意了VS2005中Web应用程序项目模板里的 ...

  7. ashx页面中context.Session[xxx]获取不到值的解决办法

    1.在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString( ...

  8. ashx+jQuery,一个轻量级的asp.net ajax解决方案

    跟shotdog老师研究探讨了下asp.net里,除官方庞大asp.net ajax之外的ajax解决方案.我们想法是以不同的服务器端方式输出,然后在页面使用jQuery的ajax实现调用服务器端几个 ...

  9. ashx是什么文件,如何创建

    .ashx 文件用于写web handler的.其实就是带HTML和C#的混合文件.当然你完全可以用.aspx 的文件后缀.使用.ashx 可以让你专注于编程而不用管相关的WEB技术..ashx必须包 ...

  10. aspx 与 ashx cs

    1. aspx 与 ashx 我们知道 aspx :继承自 System.Web.UI.Page 然而Page:IHttpHandler public class Page : TemplateCon ...

最新文章

  1. linux网络编程学习笔记之三 -----多进程并发服务端
  2. SQL数据库的数据体系结构
  3. Python 爬取简单网页
  4. numpy.random.rand、numpy.random.randn
  5. noj一道简单的数学题
  6. Authentication和Authorization的区别
  7. .NET Core with 微服务 - Elastic APM
  8. Asp.Net Boilerplate微服务实战(二)架构解析
  9. 消息存储服务器吗,消息服务器 消息存储
  10. guns企业高级单体版(前后端不分离)运行启动
  11. Redis 入门指南 pdf
  12. C# 文本操作类 Trim() 和Replace()的用法小例子
  13. C++习题 对象数组输入与输出
  14. 拓端tecdat|用Python进行图像模糊处理和特征提取
  15. 《我喜欢生命本来的样子》周国平 .mobi .epub .pdf .azw3 电子版下载 读书笔记
  16. 如何使用checkstyle添加注解_如何使用企业微信接受微信好友申请?如何用个人微信和企业微信同时添加客户?...
  17. 数仓建模—数据驱动业务
  18. Android Remote Service
  19. 【烈日炎炎战后端】设计模式(1.1万字)
  20. K3ERP web登录问题解决

热门文章

  1. 论文笔记--跨模态间的人脸与人名对齐方法研究-2012
  2. TCP/IP协议简介
  3. freebsd 6.2 安装配置笔记[转]
  4. Arcgis javascript那些事儿(十九)——地图标注添加
  5. Arcgis javascript那些事儿(十六)——GP服务的发布与使用
  6. SQL Developer显示多个工作表
  7. Starling 性能优化方案
  8. 【自我救赎--牛客网Top101 4天刷题计划】 第一天 热身运动
  9. matlab中求方差的,matlab中求方差为什么除以n-1?
  10. Navicat for MySQL 1130 - Host ‘DeskTop-**‘ is not allowed to connect to this MySQL-server错误解决