asp.net ASHX General Handler tutorial

Source: Internet
Author: User
Tags object model

You don't want to create an ordinary asp.net Web Forms page. It also returns a dynamic picture, XML, or non-HTML page through a query string. This is a simple tutorial written in the C # programming language using ASHX (general handlers).


Brief Introduction

First, let's review the purpose of using the Ashx file. Maybe we want to use this ashx file to dynamically return the data content in the URL. The URL query string we use is as follows: Http://www.dotnetperls.com/?file=name.

Getting started: How to add a ashx file.
Open your ASP.net website, click on the "Add New Item" menu on the site, open the "Add New Item" window, select the general handler (Generic Handler), and you will see a handler.ashx file with some code.

Automatically generated code
We note that the code generated automatically in the Ashx file defines the 2 methods of the IHttpHandler interface. The most important method is ProcessRequest (), whether it is a request or an output, this method is called. You should not modify any of the default inherited interface members.

URL mapping
In general, it is possible to replace the old URL or path with a new URL. For backwards compatibility and SEO, on your site, you might want to replace an old URL with a new handler. You can resolve this problem by using URL mappings. Of course, you can also rewrite the method with more complex paths.

Part of the code in the Web.config

<system.web>

<urlmappings enabled= "true" >

<add url= "~/default.aspx" mappedurl= "~/" Handler.ashx "/>

</urlMappings> ...

</system.web>

The above code will automatically transfer one connection to the other. When the Default.aspx page is requested, the Handler.ashx file is replaced. This means that your directory default page will automatically navigate to the general handler.

Add Sample Picture
Here, you may use ashx files to process picture files. Find your favorite picture on your desktop or on the web and add it to your project. For example, the picture I chose is "flower1.png", and I will use this image in the ashx file below.

modifying handler.ashx files
Your handler has 2 methods, we must modify the ProcessRequest () method, we can modify the contenttype and output of the file. You can modify your picture file name and ContentType properties by following similar code.

public void ProcessRequest (HttpContext context)

{context

   . Response.ContentType = "Image/png";

   Context. Response.WriteFile ("~/images/flower1.png");

}
public bool IsReusable

{

    get

{return

    false;

}

}

Test handlers
Test the ashx file locally. Click on the Green Run button on the website. You will see your picture file in the browser. This is the result of the picture returned by the output in the handler.


Add features
To the present position, the above example is of no effect. The main thing is to let us process a picture file through Ashx. You can add any logical code or logical reference to the Ashx file. Developers often need to use a collection of query strings requested. You can request a query string like the one used in the aspx file: request.querystring["file".

The above code receives a request and then returns a different picture file, based on a collection of query strings. He will return one of the two pictures through the query string.
For example:

url = Http://www.dotnetperls.com/?file=logo

file query String:logo

file written:Logo1.png

url = http:// Www.dotnetperls.com/?file=flower

file Query string:flower

file written:Flower1.png

Test Query string
have done so much preparatory work. Let's start the test. Open your browser and add the above query string to the URL path. You will see asp.net from the Default.aspx page to the Handler.ashx page. And he will return the appropriate file by getting the query variable.

Use
The code can be used as a counter for the number of visitors or as a number counter recommended by the log. Because of the differences between browsers and bots, this will provide more accurate access than the server log.

Performance
You might want to know if using a ashx file will bring some performance benefits or changes. ASHX files are not complex and do not involve calls to more events. As you might expect, a single request handling 10 of events consumes more performance than handling an event. So, if possible, use the Ashx file, he will bring some performance advantages.

Select a handler
When do I use a client-side handler, and when do I work with a Web Forms file? Generic handlers have a greater advantage in processing binary data, while Web forms are better at doing rapid development.

Control tree
In the ASP.net framework, Web Forms use a concept called a control tree (the Web page is saved in an object model). When you don't need the client's control tree or the entire Web site architecture, use a generic handler, which brings better performance advantages and simple code debugging.

IsReusable Property
I don't know the role of the isreusable attribute in asp.net. By reading the table name, he can improve performance and reduce memory pressure without disrupting the repetitive process.

Summarize
In this article, you'll be taught how to use ASHX custom handlers in your own web site. By modifying it, you can populate more important websites. Combining the URL mapping of a custom query string can greatly simplify and optimize your site's background code.


Do an. aspx page:

<%@ 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  
">  

This example is the case, there are three links, respectively, 1, 2, 3, click to display the corresponding picture.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.