【发布时间】:2022-11-23 20:46:05
【问题描述】:
我的问题是能够通过 post 方法检索返回值(用户选择的基金名称),以便在我的 get 方法中使用它。此值将是我的 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=***;",
}
发布和获取方法:
[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);
}
}
我不知道你会理解我的想法,但与数据库的连接取决于用户所属的基金,如果是管理员,他选择他想要指向的基金“发送到 API,我得到这个名字我将它传递给我的 get 方法。
【问题讨论】:
-
所以你想在你的
POST中使用你的Get的值,或者......? -
@CthenB 来自我的 GET 中的 POST
标签: c# asp.net-mvc