关于ashx的基本应用

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

下面是正文:

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

1、显示图片

aspx代码:

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

ashx代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Handler
{
    /// <summary>
    /// Handler2 的摘要说明
    /// </summary>
    public class Handler2 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/png";
            //context.Response.Write("Hello World");
            context.Response.WriteFile("~/Images/1.jpg");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

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

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

2、文字输出

aspx代码:

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

ashx代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace WebApplication1.Handler
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("alert('hi')");
            context.Response.Write("document.write('HELLO WORLD!!')");
        }

        public bool IsReusable
        {
            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页面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;

namespace WebApplication1.Handler
{
    /// <summary>
    /// Handler3 的摘要说明
    /// </summary>
    public class Handler3 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            // Set up the response settings
            context.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 = new MemoryStream(html2bytes);

            if (stream == null)
                stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("<html><body>get Nothing!</body></html>"));

            //Write text stream to the response stream
            const 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 bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

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

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

原文地址:https://www.cnblogs.com/ygyxinyu/p/2918961.html