Reputation: 10139
I am completely new to .ashx and Http Handlers. But, what I want to do is call a method that's defined in my .ashx file from within my controller.
For example, I have my .ashx defined as follows:
public class proxy : IHttpHandler {
public void ProcessRequest (HttpContext context) {
HttpResponse response = context.Response;
...
Now, in my controller, I want to do something like:
[HttpPost]
public int PostPicture(HttpRequestMessage msg)
{
proxy.ProcessRequest(...);
...
I know that you can call the ashx by navigating to the URL (http://server/proxy.ashx?ProcessRequest
), but I don't think this is what I need. I want to call the ashx method from inside my controller. Forgive me if this is not a recommended approach- as I said, I'm new to ashx and not sure of the appropriate way to implement them.
Upvotes: 1
Views: 2057
Reputation: 141
Here are two ways to call pass current httpcontext which in Controller to .ashx :
HttpContext context = HttpContext.ApplicationInstance.Context;
HttpContext context = (HttpContext)HttpContext.GetService(typeof(HttpContext));
Then you could call it :
[HttpPost]
public int PostPicture(HttpRequestMessage msg)
{
HttpContext context = HttpContext.ApplicationInstance.Context;
proxy.ProcessRequest(context);
Upvotes: 0
Reputation: 21485
You should extract the logic from your proxy
class in another helper class. That extracted method should not have a direct reference to HttpContext
but just the required data, for example, byte[] imageData
. Call this method from both places in your code (assuming you need to keep the handler for compatibility).
Upvotes: 3