Some ASP.NET files are dynamic. They are generated with C# code or disk resources. These files do not require web forms. Instead, an ASHX generic handler is ideal.
Go to the Website menu and select "Add New Item". Select the "Generic Handler" item, and you will get a new file with some code in it called Handler.ashx.
We use the ASHX file in a URL, and have it dynamically return content. The ASHX file handler will return data based on the query string
.
ProcessRequest()
, which will be invoked whenever the Handler.ashx file is requested.interface
inheritance or remove either of the members.http://www.dotnetperls.com/?file=name
It is often desirable to map an older URL or path to your new ASHX file. For backwards compatibility, you will want the new handler to take over an old URL in your site.
urlMappings
. Alternatively you can use more complex RewritePath
methods.Default.aspx
page is requested, your Handler.ashx file will take over.<system.web> <urlMappings enabled="true"> <add url="~/Default.aspx" mappedUrl="~/Handler.ashx"/> </urlMappings> ...
Your handler has two parts. Here we modify the ProcessRequest
method. We can change the ContentType
of the file and the Response content.
ContentType
and file name.<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { // Comment out these lines first: // context.Response.ContentType = "text/plain"; // context.Response.Write("Hello World"); context.Response.ContentType = "image/png"; context.Response.WriteFile("~/Flower1.png"); } public bool IsReusable { get { return false; } } }
The example here so far is relatively useless. All it does is allow us to pipe an image through an ASHX handler.
QueryString
collection on the Request.Request.QueryString
in the Handler just like you would on any ASPX web form page. The code is the same.<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { HttpResponse r = context.Response; r.ContentType = "image/png"; // // Write the requested image // string file = context.Request.QueryString["file"]; if (file == "logo") { r.WriteFile("Logo1.png"); } else { r.WriteFile("Flower1.png"); } } public bool IsReusable { get { return false; } } }
The above code receives requests and then returns a different file based on the QueryString
collection value. It will return one of 2 images from the 2 query strings.
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
The code here could be used as a hit tracker that counts visitors and logs referrers. This could provide a more accurate visit count than server logs.
ASP.NET web forms have many events and a detailed initialization and event model. You don't need that for dynamic images, XML, or binary files.
Is there any performance advantage or change to using ASHX files? These files are less complex. They are more streamlined and involve less code.
We used ASHX custom handlers in an ASP.NET website. We combined urlMappings
with query strings on custom handlers to greatly simplify and streamline back-end web site code.