Search

ASHX file Handler

In ASP.NET, you probably spend most of your time creating .aspx files with .cs files as code behind or use .ascx files for your controls and .asmx files for web services.
A web handler file works just like an aspx file except you are one step back away from the messy browser level where HTML and C# mix. One reason you would write an .ashx file instead of an .aspx file is that your output is not going to a browser but to an xml-consuming client of some kind.

Working with .ashx keeps you away from all the browser technology you don't need in this case. Notice that you have to include the IsReusable property.

What does the code there do?

It defines two parts of the IHttpHandler interface. The important part is ProcessRequest(), which will be invoked whenever the Handler.ashx file is requested or pointed to.

      
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}
public bool IsReusable
{
get{ return false; }
}
}


Using query strings:

Developers commonly need to use the QueryString collection on the Request. You can use the Request.QueryString in the Handler just like you would on any ASPX web form page.

      
<%@ WebHandler Language="C#" Class="QueryStringHandler" %>
using System;
using System.Web;
public class QueryStringHandler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
HttpResponse r = context.Response;
r.ContentType = "image/png";
string file = context.Request.QueryString["file"];
if (file == "Arrow")
{
r.WriteFile("Arrow.gif");
}
else
{
r.WriteFile("Image.gif");
}
}
public bool IsReusable
{
get{ return false; }
}
}





Now Debug the application:

When you pass Fille='Arrow' in query string like as follows

http://localhost:1372/FileHandler/QueryStringHandler.ashx?file=Arrow You will get the folowing output:
Image 1.JPG
Otherwise you will get following output:
Image2.JPG
http://localhost:1372/FileHandler/QueryStringHandler.ashx?file=Image

The above code receives requests and then returns a different file based on the QueryString collection value. It will return one of two images from the two query strings.

When we use Handler:

Here I want to propose some guidelines about when to use custom handlers and when to use ASPX web form pages.
Handlers are better for binary data, and web forms are best for rapid development.

Use web forms (ASPX) when you have:


  • Simple HTML pages
  • ASP.NET custom controls
  • Simple dynamic pages

Use handlers (ASHX) when you have:


  • Binary files
  • Dynamic image views
  • Performance-critical web pages
  • XML files
  • Minimal web pages

Callback in C#

In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.
Lets see how we can implement the same in C# using Action(function object).

In the below example we have a class called CallbackExample with a DoSomething() method which counts from 0 to 9 with a delay of 1 second. Within an application we can't wait for 10 seconds for the process to completed that why we implement a callback called Callback which takes an object as parameter and this been invoked once the for loop completes counting from 0-9.
There is an important check to see if the callback is not null before invoking it, this handles a situation when callback is not been registered in the other end.
The main method registered the Callaback to display a message into the console before it calls the DoSomething method.



Assign a Record to a Team in CRM 2011

This sample shows how to assign a record to a team using the AssignRequest message.