Home > Back-end >  How to use the return value in another fonction
How to use the return value in another fonction

Time:11-23

My problem with my is to be able to retrieve the returned value (the name of the fund chosen by the user) by the post method in order to use it in my get method. this value will be the name of my ConnectionName

ConnectionName :

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=.\\SQLEXPRESS; Database=Ctisn; Trusted_Connection=True; MultipleActiveResultSets=True;",
    "MECLESINE": "Server=myserver; Database=aicha_meclesine; User ID=***; Password=***;",
    "FONEES": "Server=myserver; Database=aicha_fonees; User ID=***; Password=***;",
    "MECFP": "Server=myserver; Database=aaicha_mecfp; User ID=***; Password=***;",
    "MECCT": "Server=myserver; Database=aicha_ct; User ID=***; Password=***;",
    "JSR": "Server=myserver; Database=aicha_jsr; User ID=***; Password=***;",
}

Post and Get Methods :

[Authorize]
[Route("api/[controller]")]
[ApiController]
public class TopClientsController : ControllerBase
{
    private readonly IConfiguration \_configuration;

    public TopClientsController(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    
    [HttpPost("{AdminValue}")]
    public JsonResult Post(string AdminValue)
    {
        return new JsonResult(new { data = AdminValue });
    }

    [HttpGet]
    public JsonResult Get()
    {
        string query = @"
        -------------------My sql requet-----------------
        ";
        var iden;

        if (User.IsInRole("Administrator"))
        {
            // iden = The result of the post methode  ;
        }
        else
        {
            iden=((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirst("caisse").Value;
        }


        DataTable table = new DataTable();
        string sqlDataSource = _configuration.GetConnectionString($"{iden}");
        MySqlDataReader myReader;
        using (MySqlConnection mycon = new MySqlConnection(sqlDataSource))
        {
            mycon.Open();
            using (MySqlCommand myCommand = new MySqlCommand(query, mycon))
            {
                myReader = myCommand.ExecuteReader();
                table.Load(myReader);

                myReader.Close();
                mycon.Close();
            }
        }

        return new JsonResult(table);
    }
}

I don't know will you understand my idea, but the connection to the database depends on the fund the user belongs to and if it's the admin, he chooses the fund he wants to point to 'send to the API and I get this name I pass it to my get method.

CodePudding user response:

You can't do what you're asking (or, rather, shouldn't). There's no guarantee in an API that someone will always do a POST before then calling a GET. What you can do is provide the parameter in the GET request. Ideally speaking, though, for anything concerning priviliges, you want to also add a password or other form of authentication to prevent abuse.

[HttpGet]
public JsonResult Get([FromUri] string admin)
{
}

CodePudding user response:

The controller's action is a method. You can do a classic method call :

[HttpGet]
public JsonResult Get()
{
    ...
    var postResult = Post("foo");
    ...
}
  • Related