Question

I have a generic handler that I intend to use to accept an HTTP POST from Sage Pay. I am giving them the url for the POST however it is giving an error 500 (internal server error) which I guess is something wrong on my end. If I want the generic handler to accept this POST and reply with a response is there anything special that I need to do?

I noticed in my web.config under the tags there is only a reference to a .asmx file. Do I need to add anything here?

My web.config section looks like this:

<httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>

My generic handler code looks like this. There are other clauses in there to make sure something is returned no matter what Sage send just to test that the handler is working. It seems not:

public class SagePayHandler : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        string status = context.Request.Params["Status"];
        string statusDetail = context.Request.Params["StatusDetail"];

        switch (status.ToUpper())
        {
            case "OK":
                {
                    StringBuilder content = new StringBuilder();

                    content.Append("Status=" + HttpUtility.UrlEncode("OK"));
                    content.Append("&RedirectURL=" + HttpUtility.UrlEncode("http://fooIp:80/Success.aspx?Code=SUCCESS"));
                    content.Append("&StatusDetail=" + HttpUtility.UrlEncode("OK"));

                    //SAGE TEST
                    context.Response.ContentType = "application/x-www-form-urlencoded"; ;
                    context.Response.ContentEncoding = System.Text.Encoding.Default;
                    context.Response.Write(content.ToString());
                    break;
                }
        }
   }
}
Was it helpful?

Solution

You need to register your Http Handler in the web.config.

This is configuration of IIS earlier than 7.0

<configuration>
    ...
    <system.web>
        ...
        <httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
            <add verb="POST" path="SagePay.ashx" type="<Your Handler Namespace>.SagePayHandler, <Your Handler Assembly>" validate="false"/>
        </httpHandlers>
        <httpModules>
           <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </httpModules>
    </system.web>
    ...
</configuration>

And this is configuration for IIS 7.0:

<configuration>
    ...
    <system.webServer>
        ...
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <remove name="ScriptHandlerFactory"/>
            <remove name="ScriptHandlerFactoryAppServices"/>
            <remove name="ScriptResource"/>
            <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="SagePayHandler" verb="POST" path="SagePay.ashx" type="<Your Handler Namespace>.SagePayHandler, <Your Handler Assembly>"/>
        </handlers>
        <modules>
            <remove name="ScriptModule"/>
            <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </modules>
    </system.webServer>
    ...
</configuration>

Of course the important part are the lines refering to your Http Handler.

After this registration your handler will be availble under following URL:

http:\\your.website.com\SagePay.ashx

You can read more about registering HTTP Handlers in following article: How to: Register HTTP Handlers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top