如何在WEB中Post API RESTFULL随机信用卡号

How to Post in a WEB API RESTFULL random credit card number

我对 C# 编码或根本不熟悉编码,我正在尝试创建一个 API RESTFULL,当用户 POST 它的电子邮件时,它会自动创建一个 simples 信用卡数字。我正处于 POST 函数的最开始。

// POST: api/Clientes

    [HttpPost]
    public async Task<ActionResult<Cliente>> PostCliente(Cliente cliente)
    {
        _context.Clientes.Add(cliente);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetCliente", new { id = cliente.Id }, cliente);
        
    }

所以我的问题是,生成 16 号并在 api 到 POST 中注册的代码行是什么? 提前感谢您的帮助。

第 2 部分

感谢您的解决方案,它部分起作用了。但是,当我将它部署到 Postman 中时,即使我能够注册 ID - EMAIL - 信用卡号(这很好,我仍然收到一条错误消息,如下所示。你知道我怎么不能得到这个吗消息?

System.InvalidOperationException: No route matches the supplied values. at Microsoft.AspNetCore.Mvc.CreatedAtActionResult.OnFormatting(ActionContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsyncCore(ActionContext context, ObjectResult result, Type objectType, Object value)

(留言太长我没法post全部放在这里)

如果你在Clienteclass中有一个属性“CreditCardNumber”,基本上可以这样写

[HttpPost]
public async Task<ActionResult<Cliente>> PostCliente(Cliente cliente)
{
    //Begin
    Random rnd = new Random();
    int cardNumber1 = rnd.Next(4572, 4999);
    int cardNumber2 = rnd.Next(1000, 9999);
    int cardNumber3 = rnd.Next(1000, 9999);
    int cardNumber4 = rnd.Next(1000, 9999);
    cliente.CreditCardNumber = $"{cardNumber1} {cardNumber2} {cardNumber3} {cardNumber4}";
    //End
    _context.Clientes.Add(cliente);
    await _context.SaveChangesAsync();

    return CreatedAtAction("GetCliente", new { id = cliente.Id }, cliente);
    
}