处理HTTP POST的通用处理程序

时间:2012-11-09 09:17:26

标签: c# asp.net generic-handler

我有一个通用的处理程序,我打算用来接受来自Sage Pay的HTTP POST。我给他们POST的网址然而它给出了一个错误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;
                }
        }
   }
}

1 个答案:

答案 0 :(得分:1)

您需要在web.config注册您的Http Handler。

这是早于7.0的IIS配置

<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 Handler的行。

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

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

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