ash怎么玩,關于ashx的基本應用

 2023-11-18 阅读 26 评论 0

摘要:因為公司項目的原因,對里面的一些ashx文件不是很明白,以前也從來沒接觸過。所以自己到網上查了下,然后自己寫了一些例子,在此做個記錄,也順便給初學的人提供一些方便。 下面是正文: 對于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 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 = 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 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 bool IsReusable{get{return false;}}}
}

在上面這個例子中,<body>中的內容完全可以自己根據需要進行整理,比如從數據庫獲取。

就不一一介紹了,總之ashx可以讓代碼更加整潔,在很多時候還是很方便的,比如顯示圖片的時候。

轉載于:https://www.cnblogs.com/ygyxinyu/archive/2013/02/20/2918961.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/175603.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息