HttpHandlers in ASP.NET

I just started playing around with HTTP Handlers in ASP.NET for the first time. A couple of observations:

1) The stuff is rather under-documented. In VS2002, it was practically undocumented. Things seemed to have improved a bit in VS2003. Now, I'm not saying that there not good info out there. Fritz's book covers it, as does Nikhil's, and I'm sure there's other stuff out there. But the info in the .NET framework docs themselves, which is the first place I always go, is pretty thin. Case in point - search the framework docs for "webhandler" (the ashx file directive). Nary a reference to be found. It is mentioned in a couple of MSDN articles though.

2) So far every ASHX discussion and sample I've seen shows the code for the HTTP handler directly in the ASHX file. This was so consistent it made me wonder if ASP.NET even supported the code-behind model for ASHX files. A little experimenting showed that it does - just reference the code-behind class in the Class attribute of the WebHandler directive.

I don't know why nobody mentions this. Especially since VS.NET seems to know nothing about ASHX files. When you're coding in one, you lose all syntax coloring, intellisense, automatic formatting, etc. Not terribly pleasant.

 

12 Comments

  • I have a Mac and am trying to access a site with an ashx file. It locks my computer up and shuts down the internet every time. Someone said it may be because the Mac can't read the ashx file and doesn't know what to do with it, so it kind of shrugs and dumps it. Can you tell me why and if there is anything I can do to make the Mac read this page? It's very frustrating. I get no prompt from the Mac to download a program to read the file, it just freezes on that page. I'm sorry if this isn't your area of expertise, but I'm just searching the web trying to find an answer.

  • Do you happen to have an example of what you mean by "just reference the code-behind class in the Class attribute of the WebHandler directive." ? I'm doing it, and I get a parser error, "Parser Error Message: Could not create type 'Res'".



    My directive is:

    <%@ WebHandler Language="C#" Class="Res" %>



    If I change that to "Res.cs", the name of the code-behind file, I get the same error.

  • You need to put the full name of the class, including the namespace. For example



    Class="MyNamespace.MyClassName"

  • I am trying to get the code behind stuff working but am getting the same error Tom got. I am referencing the full name of the class. I figure I'm doing something stupid. My ashx file has 1 line in it which is:

    <%@ WebHandler Language="C#" Class="ASHTest.MyTestBoxASH" %>



    and I get

    Could not create type 'ASHTest.MyTestBoxASH'.



    Any Ideas?

  • It's too bad Kevin didn't include an example to show us how he managed to use a CodeBehind file for ASHX files, because I'm as confused as the rest of you in that respect.

  • Ok, well, I've tried placing the 'codebehind' file in the bin folder, in the application root folder, etc., but simply referencing the name of the class in the ASHX file just isn't working. Tried fully qualifying the class name like you recommend and everything -- just isn't working. Perhaps some more specific information would be helpful.

  • Ah hah!



    I think you're getting CodeBehind confused with dynamically loaded assemblies. :P At least, as far as I can tell.



    I removed the httphandler code from the ASHX file and stuck it in a compiled assembly, then copied the assembly DLL over to the web application's BIN folder, and ran it -- and yes, that worked. :)



    The reason it worked is because when .NET compiled the ASHX file, it saw the reference to the class in the class= tag, and noticed that the class didn't exist in the ASHX file itself. So it looked in the web application's dynamically loaded assemblies for the class definition. When it didn't find it there, it scanned the assemblies in the BIN folder, and when it *did* find it there, it dynamically loaded my assembly into the web application's appdomain and used the implementation found there.



    *whew*



    This is great news for those of us who don't want the source code to our http handlers available to everyone we sell our products to, but at the same time don't want to mess with configuring IIS for all of our customers. :P



    Thanks for the pointers, though, Kevin. I knew this had to be possible somehow.

  • Ah, now I see where the confusion lay. I meant CodeBehind in the sense of "separating the code from the content file (ASHX)". I guess you meant it as "dynamic compilation of source code". As you discovered, the code needs to exist in a compiled assembly somewhere in the web project. In my case, that was in a server control. If you use the standard code-separation mode of VS.NET, then the web project always has its own assembly, and the class can just goe there.

  • Yeah. =)



    Actually as it turns out, we're both right... =P Read someone else's comments on the subject, and you can apparently have a true ASPX style codebehind just by supplying the 'codebehind' attribute along with the 'class' attribute in the ASHX file.



    I haven't tested this yet, but if it works like that, then cool. =P



    I'll still be compiling my asynchandler into an assembly, though -- I don't like leaving such vital bits of my application out in the open for any customer to tamper with (subsequently yelling for tech support =P).



    Thanks for the help though! =) Was a learning experience indeed. =P

  • Could not find any docs on @ WebHandler nor the System.Web.UI.SimpleHandlerFactory at all.



    This is in the machine.config for .ashx handler.

    Is the System.Web.UI.SimpleHandlerFactory is "hidden class" and used in the .NET infrastructure only. I could not find this class under the System.Web.UI namespace at all.



    Any hints/docs/guidelines would be appreciated.

  • Hello Kevin

    I'm trying my hands on a project that requires me to use 'ashx' files. However, I've not a hint of what needs to be done for that. I got a bit of hint on what you and Erik were talking about but didn't get much of it. I would appreciate if you could take me through each and every step, like how should we start? what kind of project that needs to be taken into consideration while working on ASHX files? what configuration should be done in order to achieve error free deployment of the web-application? What steps need to be taken for deploying an application using ASHX files.

    -Aditya

  • For those encountering errors (eg. "Could not create type...") when trying to use the codebehind attribute, try putting your code-behind code inside the App_Code folder. Worked for me.

Comments have been disabled for this content.