Creating an ashx handler in ASP. netposted by admin under ASP. NET articles
Today a typical web application is not only about serving HTML to browsers, but also serving XML and other types of content. say you are creating a RSS feed for your site, for example. RSS shoshould be presented as XML and I will cover that specific case in another article (will be available in related articles when posted) but this participates article will just show you the basics of putting an ashx page handler up.
Problem
We want to create a page on our site returning an XML file to the visitor.
Possible Solution
There are lots of suggestions creating a regular ASPX page (say RSS. aspx) and in page_load use response. Write to return the XML. Example:
Public class RSS: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
Response. contenttype = "text/XML ";
Response. contentencoding = encoding. utf8;
String sxml = buildxmlstring (); // not showing this function,
// But it creates the XML string
Response. Write (sxml );
Response. End ();
}
}
This will indeed work, and since it is a regular page you have great options to improve performance just with some page declarations at the top of the RSS. aspx file:
<! -- Outputcache duration = "600" varybyparam = "NONE" -->
Now what's wrong with it? Well, basically nothing, but it is a fact that our page class (RSS) inherits from system. web. UI. page and that page handler is indented for serving aspx pages (I. E webforms) and between des a lot of overhead not needed for just serving a simple XML file.
Alternate Solution
There are other solutions. you can create your own page handlers, and you can even map your own file Extentions to your handlers, for example you cocould invent your own file extention called. myspecialrss. now you can (with some implementations settings in IIS/web. config) map all requests to whatever. myspecialrss to your own special file handler. however, needing to do some comprehensions are never fun to do-if even possible in a shared hosting scenario, therefore Microsoft has kindly given us a special filetype. ashx which maps to the ASP. net engine.
Now what we are going to is create a file-RSS. ashx-which when called returns the same XML as in the example abve. if you have forgotten why-The ashx handler doesn't give us all that overhead as. aspx request does.
So, start off by creating
RSS. ashx
<! -- Webhandler Language = "C #" class = "kbmentor2.rsshandler" -->
Yes, thats correct, just one single line. it says the class for our webhandler is called kbmentor2.rsshandler, so we will now create that class. to put it simple-what happens is that when the request for RSS. ashx comes to the ASP. net engine it reads the RSS. ashx file, sees that the class is kbmentor2.rsshandler and therefore instantiates an object of that class.
Now lets have a look at the handler class:
Rsshandler. CS
Namespace kbmentor2
{
Using system;
Using system. IO;
Using system. Web;
Public class rsshandler: ihttphandler
{
Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/XML ";
Context. response. contentencoding = system. Text. encoding. utf8;
String sxml = buildxmlstring (); // not showing this function,
// But it creates the XML string
Context. response. cache. setexpires (datetime. Now. addseconds (600 ));
Context. response. cache. setcacheability (httpcacheability. Public );
Context. response. Write (sxml );
}
Public bool isreusable
{
Get {return true ;}
}
}
}
And there you have it. Looks pretty much like the first code we created, doesn't it?As for caching, you can solve it by accessing the cache object from your code, see the context. response. cache CILS.
From: http://www.aspcode.net/Creating-an-ASHX-handler-in-ASPNET.aspx
What is ashx and how to create it?
. Ashx File is used to write web handler. It is actually a hybrid file with HTML and C. Of course, you can use the. aspx file suffix. Use
. Ashx This allows you to focus on programming without having to worry about related web technologies.
. Ashx Isreusable must be included, as shown in the following example.
<% @ Webhandler Language = "C #" class = "averagehandler" %>
Using system;
Using system. Web;
Public class averagehandler: ihttphandler
{
Public bool isreusable
{Get {return true ;}}
Public void processrequest (httpcontext CTX)
{
CTX. response. Write ("hello ");
}
}. The advantage of ashx over. aspx is that there is no more HTML
Note the web application in vs2005ProgramThe generic handler item in the Project template finds that it is A. ashx file, in fact it is an httphandler. Later, I checked the. NET SDK document and found that ASP. net1.1 also supports. ashx, but no details are provided.
As we all know, httphandler is a method for completely customizing HTTP requests through the Web. config to define ASP.. Net runtime to filter out HTTP requests to be customized and send them to the Web. config.
Exploitation. the ashx file is a better method. This file is similar. aspx file, which can be used to call the httphandler class, thus eliminating the need for common. the control parsing and page processing process of the aspx page. This file is especially suitable for generating dynamic images, generating dynamic text, and other content.
CreateCubeMethod:
First open a web project, and then use "add" --> "Add new project" of vs2003 solution Resource Manager in any directory. In the dialog box, select "Text File ", enter "textbuilder. ashx ".
Then, in the same directory, use solution Resource Manager, use "add" --> "add class", and enter "textbuilder. ashx. cs" in the class file name ". It can be seen that its file naming rules are the same as that of. aspx files.
Enter the following in the. CS file: Code (Namespace omitted ): Using System. Web
Public Sealed Class Textbuilder: ihttphandler
{
Public Void Processrequest (httpcontext context)
{
Context. response. clearcontent ();
Context. response. contenttype= "Text/plain";
Context. response. Write ("Hello World");
Context. response. End ();
}
Public Bool Isreusable
{
Get {Return True;}
}
}
Enter the above class call code in the first line of the "textbuilder. ashx" file: <% @ Webhandler Language = " C # " Class = " Mynamespace. textbuilder " Codebehind = " Textbuilder. ashx. CS " %>
Note that the complete name of the class, including the namespace and class name, must be entered in the class item.
Finally, save and compile the project.
Run the IE test and enter the. ashx address.
You can see that the response class has an outputstream method that can output binary data streams to the client. Therefore, in my project, use this method in one. in ashx, The dundaschart control can be used to generate a very good statistical chart. It can be used to send binary data, which is convenient and does not need to be stored on the web. enter any configuration code in config.
. The ashx file has a disadvantage. It is very troublesome to process the control's sending back event. For example, if it is used to generate the DataGrid list, it does not work, but it is required to process data sending back. the features of the ASPX page can only be manually processed. Therefore,. ashx is generally used to output projects that do not require sending back.
From: http://www.cnblogs.com/lin614/archive/2008/01/18/1044734.html