You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core升级后Stripe集成失效,请求模型无值问题排查

问题描述

我的Stripe集成在ASP.NET Core 2.1应用中运行正常,但升级至ASP.NET Core 3.0及后续的.NET 6.0版本后,该集成停止工作。仅有的错误信息为:

:44371/ajax/confirm_payment:1 Failed to load resource: the server responded with a status of 500 ()

我已更新Stripe.Net版本,尝试升级到3.0和6.0,但均无效果。前端通过ajax调用的/ajax/confirm_payment对应如下控制器代码:

[Route("/ajax/confirm_payment")] 
public class ConfirmPaymentController : Controller
{
    public ConfirmPaymentController(IConfiguration iConfig)
    {
        configuration = iConfig;
    }

    public IConfiguration configuration { get; set; }

    public IActionResult Index(ConfirmPaymentRequest request)
    {
        string stripeSecretKey = configuration.GetSection("appSettings").GetSection("StripeSecretKey").Value;  //Configuration["stripeSecretKey"];

        StripeConfiguration.ApiKey = stripeSecretKey;

        var oessConnectedAccount = request.ConnectedAccount;

        string desc = HttpContext.Request.Query["desc"].ToString();
        var oessDescription = request.Description;
        var callCurrency = request.PaymentCurrency;
        var oessDescriptor = request.Descriptor;
        var oessReceiptEmail = request.ArtistEmail;
        var oessPaymentAmount = request.PaymentAmount;

        Console.WriteLine("Amount:" + oessPaymentAmount);

        var paymentIntentService = new PaymentIntentService();
        PaymentIntent paymentIntent = null;

调试时发现,在.NET 6.0版本中ConfirmPaymentRequest请求对象无法获取任何值,但ASP.NET Core 2.1版本中可正常获取。请问有什么解决思路?

解决思路
  • 检查模型绑定序列化器差异
    ASP.NET Core 3.0+ 默认替换Newtonsoft.Json为System.Text.Json,如果ConfirmPaymentRequest类使用了Newtonsoft专属特性(如[JsonProperty]),或存在System.Text.Json不兼容的类型,会导致绑定失败。解决方式:

    1. 引入Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet包,在配置中切换回Newtonsoft:
      // .NET 6 Program.cs示例
      builder.Services.AddControllers().AddNewtonsoftJson();
      
    2. 调整ConfirmPaymentRequest适配System.Text.Json:用[JsonPropertyName]替代[JsonProperty],确保属性为公共可读写,避免非标准类型。
  • 明确请求绑定源与内容类型

    1. 确认前端AJAX请求的Content-Type为application/json(JSON请求)或application/x-www-form-urlencoded(表单请求),ASP.NET Core 3.0+对内容类型校验更严格。
    2. 给控制器参数添加绑定特性:JSON请求加[FromBody],表单请求加[FromForm],避免默认绑定行为歧义:
      public IActionResult Index([FromBody] ConfirmPaymentRequest request)
      
  • 修正字段名称匹配问题
    System.Text.Json默认区分大小写,而Newtonsoft默认不区分。如果前端字段是驼峰命名(如connectedAccount),类属性是PascalCase(如ConnectedAccount),可配置忽略大小写:

    // .NET 6 Program.cs示例
    builder.Services.AddControllers().AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
    });
    
  • 启用详细日志排查500错误
    在appsettings.json中调高日志级别,获取具体错误信息:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Debug"
        }
      }
    }
    

    查看控制台或日志文件,会暴露模型绑定失败的具体原因(如字段类型不匹配、必填项缺失等)。

  • 确认路由与请求方法匹配
    给控制器方法明确指定HTTP方法特性([HttpPost]/[HttpGet]),避免ASP.NET Core版本间路由默认行为差异导致的不匹配:

    [HttpPost]
    public IActionResult Index([FromBody] ConfirmPaymentRequest request)
    

内容的提问来源于stack exchange,提问作者Andy Laffan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 03:50:04