1

我有一个通用处理程序,我打算用它来接受来自 Sage Pay 的 HTTP POST。我给他们 POST 的 url 但是它给出了一个错误 500(内部服务器错误),我猜这对我来说是错误的。如果我希望通用处理程序接受此 POST 并回复响应,我需要做些什么特别的事情吗?

我注意到在标签下的 web.config 中只有一个 .asmx 文件的引用。我需要在这里添加什么吗?

我的 web.config 部分如下所示:

<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>

我的通用处理程序代码如下所示。那里还有其他子句来确保无论 Sage 发送什么内容都会返回某些内容,只是为了测试处理程序是否正常工作。似乎没有:

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;
                }
        }
   }
}
4

1 回答 1

1

您需要在web.config.

这是 IIS 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>

这是 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>

当然,重要的部分是引用您的 Http 处理程序的行。

注册后,您的处理程序将在以下 URL 下可用:

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

您可以在以下文章中阅读有关注册 HTTP 处理程序的更多信息:如何:注册 HTTP 处理程序

于 2012-11-09T09:58:40.547 回答