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")
Related
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.
Is there a way in ASP.net to fetch data from database using Jquery Ajax Method. I know that i can easily access the data by creating a webservice and get it from there using Ajax.
What I want to know is that how can I get the data from simple class methods which accesses the database rather than creating a webservice.
One way can be is to write a HTTPHandler and call that handler using Jquery to retrieve the data.
Check this out for a super-easy (lazy) way of doing a return straight from a public method defined in your aspx.cs file:
asp.net web forms json return result
Example shows putting [WebMethod] and [ScriptMethod(ResponseFormat = ResponseFormat.Json)] attributes on top of the method allows you to call directly from ajax client side request. Worth considering securing this method though to prevent calls from elsewhere!
In ASP.Net, is it possible to pass parameters to the "New" constructor of a User Control class? In VB.Net:
Public Sub New(ByVal pRequiredParam As String)
'Do something with required Param
End Sub
When I use this user control in a generic ASP.Net page, it doesn't prompt me for "pRequiredParam". Of course, if this was a "normal" class, I would have to supply "pRequiredParam" when I instantiate the object. Is this not possible with a User Control?
While you can certainly create a custom constructor like the one you show in your code sample, you cannot force the designer to make use of it. How would it determine what to send as argument to the constructor?
However, you can load the control dynamically in your code-behind file, and then use whatever parameters it defines. Note though that if you want the control to also work well with the graphical designer, I think that the control need to have a public constructor that takes no parameters (but I think you get that automatically in VB.NET?)
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
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...