c# - 未找到 Stripe Webhook Controller

标签 c# .net asp.net-mvc asp.net-web-api stripe-payments

我有以下内容:

public class StripeController : Controller
{

    private readonly UserService _userService;

    public StripeController(UserService userService)
    {
        _userService = userService;
    }

    [HttpPost]

    public ActionResult StripeWebook()
    {
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }


    [HttpPost]
    [Route("api/stripewebhook")]
    public async Task<ActionResult> Index(CancellationToken ct)
    {
        var json = new StreamReader(Request.InputStream).ReadToEnd();

        var stripeEvent = StripeEventUtility.ParseEvent(json);

        switch (stripeEvent.Type)
        {
            case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
                var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
                break;
        }

        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }


}

来自 stripe 的请求会产生错误:

The controller for path '/api/stripewebhook' was not found or does not implement IController

知道为什么当我从 strip 门户测试时会发生这种情况吗?

最佳答案

使用 WebApi 2 没问题。

这是最小的 WebApi Controller :

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;

    namespace WebApplication1.Controllers
    {
        public class StripeController : ApiController
        {
            [HttpPost]
            [Route("api/stripewebhook")]
            public IHttpActionResult Index()
            {
                var json = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
                return Ok();
            }
        }
    }

如果您从 VS 执行此操作,您可以从 http://localhost:(port)/api/stripewebhook 访问它

现在您只需要扩展它以包含 Stripe 代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;

    namespace WebApplication1.Controllers
    {
        public class StripeController : ApiController
        {
            [HttpPost]
            [Route("api/stripewebhook")]
            public IHttpActionResult Index()
            {
                var json = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();

                var stripeEvent = StripeEventUtility.ParseEvent(json);

                switch (stripeEvent.Type)
                {
                    case StripeEvents.ChargeRefunded:  // all of the types available are listed in StripeEvents
                        var stripeCharge = Stripe.Mapper<StripeCharge>.MapFromJson(stripeEvent.Data.Object.ToString());
                        break;
                }

                return Ok();
            }
        }
    }

关于c# - 未找到 Stripe Webhook Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670242/

相关文章:

c# - C#中的别名资源文件命名空间

c# - 是否可以在没有接口(interface)通用方法的情况下初始化属性?

.net - 在 Disposable 模式中什么算作 "Unmanaged Resource"?

c# - 自定义接口(interface)实现和接口(interface)c#

c# - 如何使用 C#.net 编辑 MS 项目的 TimescaleStart

javascript - 如何在 Asp.Net MVC 中检测互联网连接以使用 Google map ?

c# - 将 short 转换为 bool Entity Framework

c# - 在 Kinect One (v2) 中设置红外灯的亮度

c# - 创建 XML 文件并赋予所有 Windows 帐户用户写入权限

javascript - 如何通过JQuery JSON数据获取Check Box(@Html.CheckBox)的值