调试器没有命中断点

Debugger doesn't hit certain breakpoint

我正在 VS2019 上开发 ASP.NET 5 Web API 项目。我试图在控制器中的 POST 操作 (PostCustomerPayment) 上设置一个断点,但是,断点永远不会被击中。我尝试在其他几个区域(例如 GET 操作和其他控制器)设置断点,它们工作得很好。几个小时以来,我一直在努力解决这个问题,我什至尝试修复视觉效果,但没有成功。任何帮助将不胜感激

控制器代码如下:

namespace IcartE1.Controllers.API
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

    public class CustomerPaymentsController : ControllerBase
    {
        private readonly ApplicationDbContext _context;
        private readonly ICipherService _cipherService;

        public CustomerPaymentsController(ApplicationDbContext context,ICipherService cipherService)
        {
            _context = context;
            _cipherService = cipherService;
        }

        // GET: api/CustomerPayments
        [HttpGet]
        public async Task<ActionResult<IEnumerable<CustomerPayment>>> GetCustomerPayment()
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var payments = await _context.CustomerPayment.Where(cp=> cp.CustomerId==userId).ToListAsync();
            foreach(var payment in payments)
            {
                payment.CardNumber = _cipherService.Decrypt(payment.CardNumber);
            }
            return payments;
        }

        // POST: api/CustomerPayments
        [HttpPost]
        public async Task<ActionResult<CustomerPayment>> PostCustomerPayment([FromForm] CustomerPayment customerPayment)
        {
            customerPayment.CustomerId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            if (ModelState.IsValid)
            {
                customerPayment.CardNumber = _cipherService.Encrypt(customerPayment.CardNumber);
                _context.CustomerPayment.Add(customerPayment);
                await _context.SaveChangesAsync();

                return CreatedAtAction("GetCustomerPayment", new { id = customerPayment.Id }, customerPayment);
            }
            return ValidationProblem();
        }

        // DELETE: api/CustomerPayments/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteCustomerPayment(int id)
        {
            var customerPayment = await _context.CustomerPayment.FindAsync(id);
            if (customerPayment == null)
            {
                return NotFound();
            }

            _context.CustomerPayment.Remove(customerPayment);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool CustomerPaymentExists(int id)
        {
            return _context.CustomerPayment.Any(e => e.Id == id);
        }
    }
}

编辑:Postman Request

相关模型

public class CustomerPayment
    {
        public int Id { get; set; }

        [Required,Display(Name ="Holder Name")]
        [RegularExpression(pattern: @"^[a-zA-Z ]+$", ErrorMessage = "Holder name can only contain letters.")]
        public string HolderName { get; set; }

        [Required,Display(Name ="Card Number")]
        [CreditCard]
        public string CardNumber { get; set; }
        [Required]
        public DateTime ExpiryDate { get; set; }
        [Required]
        public string CustomerId { get; set; }
    }

编辑 2:因为 customerId 是按要求设置的,所以操作较早处理,因此请求根本不会到达控制器,从 customerId 中删除 [Required] 注释标签修复了问题

PostCustomerPayment([FromForm] CustomerPayment customerPayment)

我怀疑这是您访问该操作的方式。由于您使用的是 [FromForm],您的请求应该是这样的;

site.com/api/CustomerPaymentsController/PostCustomerPayment?PropertyName=Test

如果你想 POST json body,请尝试将其更改为 [FromBody]

您可以在此处阅读有关模型绑定的更多信息; https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#frombody-attribute

因为 customerId 是按要求设置的,所以操作在管道中较早处理,因此请求根本不会到达控制器。从 customerId 中删除 [Required] 注释标记修复了问题。