Custom HttpHandlers and different handler types - asp.net

All literature I see on creating custom handlers deals with associating an extension with a handler, e.g. if I wanted a handler for Ajax requests, I could implement the IHttpHandler interface in an AjaxHandler class.
Now, to have individual instances of AjaxHandlers, e.g. DocAjaxHander, PersonAjaxHandler etc. how would I derive the base AJAX handling of my AjaxHandler class without registering each individual *.ajax page?

It sounds like your assuming 1 HttpHandler = 1 Page or 1 Control, but as I understand, 1 HttpHandler can handle all pages of a certain file extension.
Your Question is not very clear, and your reponse to another answerer makes no sense...
"In fact, it seemed, to me, a lot like I was asking Http handlers, using a .ajax handler as an example."
But I shall assume you are thinking "DocAjaxHander" and "PersonAjaxHandler" should each be created for a "DocAjaxControl" and "PersonAjaxControl" respectively. I dont think that would be neccesary, 1 handler should be able to handle all your ajax requests if you choose to do it that way, but it doesn't feel like the most intuitive solution to me (using HttpHandlers), anyway, onto the details...
every IHttpHandler object needs to implement :
public void ProcessRequest(HttpContext context)
which allows :
context.Response.Write("Your JSON Response in here");
but at the level of 'ProcessRequest()', you have no access to the instance of the control which created the ajax call, or to the 'System.Web.UI.Page' object that holds the control, or anything.
context.Request
to the rescue! With the Request object above you can read QueryStrings, Sessions, an you can determine the path of the original HttpRequest (i.e. PersonAjaxObject may make an ajax call to 'myPersonobjPage.ajax' for its JSON data, but the '.ajax' extention lands the request at your custom http handler and it's ProcessRequest method.)
If I was you, and I was going to use an HttpHandler for my ajax calls, I'd use query string data to provide enough info for my handler to know 'what type of object am I responding too' as well as 'what data is that object requesting'.
Hope that helps.

You can automatically handle AJAX request in a number of ways. Here's how to do it with a web service:
http://www.asp.net/AJAX/Documentation/Live/Tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

Well, one way to do it would be via query string params...

Related

Which Page is calling the Handler ashx

I want know which page and which URL has calling my Handler .ashx, is that possible?
I need this because, I have an Handler who calls and convert images from database, but some of my URLS of images are not passing the right query argument (they don't exist in database) and I need what is the URL who call to see what is the image for that arguments.
why not just use
context.Request.UrlReferrer?
A quick solution to your immediate question is to call (in C#)
Inside your public void ProcessRequest(HttpContext context){} method, add the following 3 lines.
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
String referer = worker.GetKnownRequestHeader(HttpWorkerRequest.HeaderReferer);
This will give you the URL of the page that called your handler.
To go further though, you should ideally be implementing error handling to handle any missing images.

how to access application.get from generic handler

I have a global.asax in which I define some variables that I want to be available throughout the application, such as a class to handle certain database requests. In an aspx page, I can access these variables/objects using for example in Visual Basic:
dim dataMan as clsData = application.get("dataGofer")
My question: is it possible to call this from a generic handler (.ashx file)? If so, can you please show me a code snippet? Thanks!
You have Context parameter of ProcessRequest method through which you can access the page objects.
Dim value=context.Application.Get("key")

Custom Elmah YSOD data

I'm using Elmah with ASP.NET and wondering how I would add custom data, such as a session variable, to an unhandled exception email.
I've tried several handlers in the Global.asax file but can't seem to find the right one.
For this, I'd think you would need to modify the Elmah source and recompile. It shouldn't be too difficult to achieve. If you have a look in the constructor of the Elmah.Error class, the HttpContext is passed in, from which you should be able to get the info you need, e.g. Session, Form variables etc. You could add custom fields to the Elmah.Error class for this data
I think the Elmah.ErrorMailHtmlFormatter class is where the email is constructed using a HtmlTextWriter, and here you could insert code in the RenderSummary() method to include the custom fields you added to Elmah.Error.
I know it may be a pain to start working with source, but personally I think it's the cleanest way as there currently is no facility for report/email templates, and it's better that bolting on something to change the output after it has been generated.
Andrew's answer helped a lot, thanks. I ended up doing the following:
Added a OnBuilding event to the ErrorMail http module. The event args for this event have a NameValueCollection property.
I handled the OnBuilding event in global.asax.
Since HttpModules don't always have access to sessionstate, esp. if the exception occurs before the session is loaded, I copied the data i wanted reported into the HttpApplication cache(indexed by sessionid).
When an exception occurs I grab the data i want out of the application cache via the sessionid stored in the request(specifically, in the cookie). I generate a NameValueCollection from this data and send it back to the httpmodule via the OnBuilding args.
The data is then rendered to email similarly to how the server variables section is rendered.

Why is IHttpAsyncHandler being called over IHttpHandler?

I made a custom handler that derives from MvcHandler. I have my routes using a custom RouteHandler that returns my new handler for GetHttpHandler(), and I override ProcessRequest() in my custom handler. The call to GetHttpHandler is triggering a breakpoint and my handler's constructor is definitely being called, but BeginProcessRequest() is being called on the base MvcHandler instead of ProcessRequest().
Why are the async methods being called when I haven't done anything to call them? I don't want asynchronous handling, and I certainly didn't do anything explicit to get it. My controllers all derive from Controller, not AsyncController.
I don't have the source code with me right now, but I can add it later if needed. I was hoping someone might know some of the reasons why BeginProcessRequest might be called when it's not wanted.
Brad Wilson responded to my post on the Asp.net forums with the following answer http://forums.asp.net/t/1547898.aspx:
Short answer: yes.
With the addition of AsyncController,
the MvcHandler class needs to be an
IHttpAsyncHandler now, which means
that as far as the ASP.NET core
runtime is concerned, the entry points
are now BeginProcessRequest and
EndProcessRequest, not ProcessRequest.
It sounds like ProcessRequest is not even called anymore, but I could be mistaken. I can say that I haven't seen it in my testing.

How do I call an ASHX from inside an ASPX.VB function?

I need to get a value from an API I made with ASHX and normally it is called from javascript but I need to call it right in ASP.NET I figured this shouldn't be a problem but I'm not sure the syntax.
Well you have a couple options
You can refactor the code in your ASHX to be in a shared library so you can access the methods directly and so can the handler.
You can instantiate the handler and invoke the members if they aren't private.
You can create a webrequest to the handler and handle the response.
These are just a few of the easy ways.
I personally like the first method because it promotes code reuse, but depending on scenario you can do what you like.
Edit to provide answers for question in comment.
Essentially Yes... Instead of having a bunch of code in your handler you make a class called something meaningful to you contextually. Inside that class you place the logic that was in your handler. Then from your handler you can create an instance or call a static version of the class (depending on how you implemented it) passing it the HttpContext object or whatever is required for that logic to run correctly. Do the same thing in your ASPX page. You can now call into an object that contains the logic from anywhere in your app instead of having it reside in the handler alone.
EX:
Public Class MyCommonLogic
Public Shared Function ReturnSomethingCommon(context As HttpContext) As String
Return "Hello World!"
End Function
End Class
Then from the handler or the aspx page..
Dim something As String = MyCommonLogic.ReturnSomethingCommon(...)
I made the function static, but that is just an example of course I would implement it however would make more sense in your scenario.
Changed code to VB sorry about that.
If the ASHX is on the same server especially if its within the same web app, you should refactor your logic out of the ashx into a common class that both the aspx and ashx can call.
Otherwise you can look at using: System.Net.WebClient

Resources