Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways to use ashx files

Shulou Source: shulou.com Published: 2022-06-03 07:42:11 10月06日 Update

This article mainly explains the "what is the use of ashx files", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the use of ashx files" bar!

When it comes to Ashx files, we think of http handler and image loading (we usually used ASPX or Webservice to do it before). The general practice is as follows:

Handler.ashx:

The copy code is as follows:

Using System

Using System.IO

Using System.Web

Public class Handler: IHttpHandler {

Public bool IsReusable {

Get {

Return true

}

}

Public void ProcessRequest (HttpContext context) {

Context.Response.ContentType = "image/jpeg"

Context.Response.Cache.SetCacheability (HttpCacheability.Public)

Context.Response.BufferOutput = false

PhotoSize size

Switch (context.Request.QueryString ["Size"]) {

Case "S":

Size = PhotoSize.Small

Break

Case "M":

Size = PhotoSize.Medium

Break

Case "L":

Size = PhotoSize.Large

Break

Default:

Size = PhotoSize.Original

Break

}

Int32 id =-1

Stream stream = null

If (context.Request.QueryString ["PhotoID"]! = null & & context.Request.QueryString ["PhotoID"]! = "") {

Id = Convert.ToInt32 (context.Request.QueryString ["PhotoID"])

Stream = PhotoManager.GetPhoto (id, size)

} else {

Id = Convert.ToInt32 (context.Request.QueryString ["AlbumID"])

Stream = PhotoManager.GetFirstPhoto (id, size)

}

If (stream = = null) stream = PhotoManager.GetPhoto (size)

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)

}

}

}

* .aspx:

We modify the following and find that we can output not only pictures but also text:

Handler.ashx:

The copy code is as follows:

Using System

Using System.Web

Public class Handler: IHttpHandler {

Public void ProcessRequest (HttpContext context) {

Context.Response.ContentType = "text/plain"

Context.Response.Write ("alert ('hi')")

}

Public bool IsReusable {

Get {

Return false

}

}

}

* .aspx:

Eject alert

You can also think of .ashx as a css file

Xml file

OrderDoc.load ("Handler.ashx")

You can also embed text:

Handler.ashx:

The copy code is as follows:

Using System

Using System.Web

Public class TestHandler: IHttpHandler {

Public void ProcessRequest (HttpContext context) {

Context.Response.ContentType = "text/plain"

Context.Response.Write ("[xss_clean] (\" Hello World\ ");")

}

Public bool IsReusable {

Get {

Return false

}

}

}

* .aspx:

When you want to access your Session from ashx or HttpHandler, you must implement the IReadOnlySessionState interface.

Code:

The copy code is as follows:

Using System

Using System.Web

Using System.Web.SessionState

Public class DownloadHandler: IHttpHandler, IReadOnlySessionState

{

Public bool IsReusable {get {return true;}}

Public void ProcessRequest (HttpContext ctx)

{

Ctx.Response.Write (ctx.Session ["fred"])

}

}

Thank you for your reading, the above is the content of "what is the use of ashx files?" after the study of this article, I believe you have a deeper understanding of the use of ashx files, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Code file usage method learning content picture text output practice that is ideas situations interfaces articles more knowledge knowledge points articles follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology Shulou Information Linux Shulou Tech Info