1

Generic Handler doesn't return response on hosted environment

I'm trying to host my application which is built using ASP.NET. It has a simple html file calling javascript where I have an ajax call requesting response from a Generic handler(.ashx). The application runs fine on my local but it doesn't work on hosted environment.

When I debug the javascript on hosted site, the response it is returning is

"<%@ WebHandler Language=\'C#\' CodeBehind=\"GetInfo.ashx.cs" Class=\"MyProject.GetInfo\" %> \r\n" in the success data element on ajax.

I'm new to this hosting and I'm not sure If my ajax request is wrong or If I'm publishing it wrong. I just used simple publish option on visual studio and posted all the files to hosting environment.

Here is my ajax request:

            $.ajax({
                type: "GET",
                url: "/Handlers/GetInfo.ashx",
                contentType: "application/json; character=utf-8",
                success: function (data) {
                    if (data.status == "OK") {
                     //some code here

                    }
                }
            });

Here is my handler:

GetInfo.ashx.cs

public class GetInfo: IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
       context.Response.Write(JsonConvert.SerializeObject (new { status = "OK" }));   

    }

}
CodeNinja
  • 26
  • 7
  • If you add a breakpoint to ProcessRequest does it stop there? – Ricardo Peres May 10 '19 at 15:20
  • On Local? Yes. I get response of data.status = OK as well. – CodeNinja May 10 '19 at 15:21
  • By managed do you mean Azure? Then it's because the .ashx is not being mapped to the generic handler on web.config file. – Ricardo Peres May 10 '19 at 15:27
  • I'm hosting it on a third party hosting server and the database I'm using is azure. But, I've been reading about .ashx should be added to web.config file but coulnd't find how to do it. Can you please provide me a link on how to add it? And why does it work on local? without adding .ashx to web.conifg. – CodeNinja May 10 '19 at 15:35
  • Because locally you have that on your global configuration file machine.config. See https://stackoverflow.com/questions/24056966/cant-find-httpmodules-and-httphandlers-inside-machine-config. – Ricardo Peres May 10 '19 at 15:39
  • Thank you so much Ricardo. You put me in right direction. I found this article [link](https://www.c-sharpcorner.com/uploadfile/37db1d/create-your-first-http-handler-in-Asp-Net-3-5/) . Do you mind telling me what should go in path variable in my case? – CodeNinja May 10 '19 at 15:56

1 Answers1

0

Make sure you have this in your web.config file (locally it should be in global web.config):

<system.web>
  <httpHandlers>
    <add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory, System.Web" validate="True"/>
  </httpHandlers>
</system.web>
Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
  • Thank you! It still didn't work. I tried adding this tag both inside and outside tag in web.config. Should I need any library reference for this? – CodeNinja May 10 '19 at 16:20
  • it should be inside , yes. This type belongs to the standard System.Web.dll, so you shouldn't need anything. – Ricardo Peres May 10 '19 at 16:23
  • See https://stackoverflow.com/questions/28856991/removing-unused-http-handlers-for-better-performance-security – Ricardo Peres May 10 '19 at 16:25
  • Still doesn't work. I tried many options like the ones specified here [link](https://stackoverflow.com/questions/13340635/iis-7-5-cant-load-custom-http-handler-with-codebehind-file?lq=1). Even added custom hanlder for each handler and I've also tried putting the code behind logic in the markup direclty like shown in the link I mentioned. No use. – CodeNinja May 10 '19 at 18:34