简体   繁体   中英

C# WEB API How to HttpPost with Parameter and how to httpsGet string and return custom List

I am doing a WEB API which generates a random Credit Card Number with the clients email.

First, how can I can, without Postman JSON code, Post the clients email and the credit card number and de ID just by https://localhost:44355/api/ClienteDados/"Typing the email here". This below is what I have so far. I manage to do it with Postman, but I want to do it in the URL.

My Class is ClienteDados.

Int ID

string EmailId

string NumCartao

My context is _context.

My DB is ApiContext and DbSet ClientesDB

[HttpPost("{email}")]
    public async Task<ActionResult<ClienteDados>> PostClienteDados(string email, ClienteDados clienteDados)
    {
        //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);
        clienteDados.NumCartao = $"{cardNumber1} {cardNumber2} {cardNumber3} {cardNumber4}";
        clienteDados.EmailId = email;
        //End

        
        _context.ClientesDB.Add(clienteDados);
        await _context.SaveChangesAsync();


        return CreatedAtAction("GetClienteDados", new { id = clienteDados.NumCartao }, clienteDados.NumCartao);
    }

After I registred the clientes info with EF Inmemory, I want to make a HttpGet of all credit cards from the parameter EMAIL. So when they type /email/"email here" it show a LIST of all credit card numbers in that email.

How can I do the list, and how can I do the search by the string Email? all the time I can only search by int ID.

[HttpGet, Route("email/{email}")]
    public async Task<ActionResult<IEnumerable<ClienteDados>>> GetEmail(string email)
    {


        var cc = await _context.ClientesDB.FindAsync(email);

        return Ok(cc);

    }

The Https Get above does not work yet because it's a string, it only works with the int ID. Thank you very much in advance.

EDIT 1 So I managed to get the List by the url parameter, but its showing me the id, the email and the CC Number, how can I filter it just to CC number?

List<ClienteDados> list = await _context.ClientesDB.Where(a => a.EmailId.Equals(email)).ToListAsync();
       return Ok(list);

Try This code in your GetEmail function

await _context.ClientesDB.Where(a => a.EmailId.Equals(email)).ToListAsync();

UPDATE:

First you can create a new TYPE

    class NEW_TYPE
    {
        public string CARD_ { get; set; }
    };

then inside the function use this query

                var query = from a in _context.ClientesDB
                    where a.EmailId.Equals(email)
                    select new NEW_TYPE
                    {
                        CARD_ = a.NumCartao
                    };

                var cc = await query.ToListAsync();
                return cc;

cc will be a list of NEW_TYPE which contain only card info List<NEW_TYPE>

You can reference THIS website for additional information.

Using this example:

[HttpPost]
public void Test(int age)
{
    Console.Write(age);
}

You can create a request like so: 在此处输入图像描述

C# will consume the url parameters and bring them into the call. Please note, the variable names MUST match the parameter variables for it to work. Notice how I used int age and age in both places. You need to put url params first.

In your case, you would pass the email, like so:

https://localhost:5443/controller/test?email=email@email.com

And the function in c# would have string email as the first item in the function input variables. public void Test(string email)

change this line:

List<ClienteDados> list = await _context.ClientesDB.Where(a => a.EmailId.Equals(email)).ToListAsync();
   return Ok(list);

to:

var list = _context.ClientesDB.Where(a => a.EmailId.Equals(email)).Select(s => new { NumCartao  = s.NumCartao }).ToList();
   return Ok(list);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM