For now I have a procedure I wish to call:
CALL Get_Discharge_Station('2022-02-02', 6, 11800);
I want to pass these three parameters - 2022-02-02,6,11800
to url address, when I enter different date,number and number of station to get different data.
For now my API controller look like this:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace WebAPI.Controllers
{
[Route("api/hqdataahs")]
[ApiController]
public class HQ_data_AHSController : ControllerBase
{
private readonly IConfiguration _configuration;
public HQ_data_AHSController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet]
public JsonResult Get()
{
string query = @"CALL Get_Discharge_Station('2022-02-02', 6, 11800);";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("AppCon");
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);
}
}
}
What can I change to pass there three parameters in url address when I access the controller?`
CodePudding user response:
declare the args
[HttpGet]
public JsonResult Get(string dataStr, int noodle, int frumpy)
{
now construct the call
string query = $"CALL Get_Discharge_Station('{dateStr}', {noodle}, {fumpy});"
CodePudding user response:
just fix the action
[HttpGet("~/HQ_data_AHS/GetData")]
public JsonResult Get(string date, int num, int statNum)
{
string query = $"CALL Get_Discharge_Station('{date}', {num}, {statNum});";
.....
and carefully read all comments below about sql injection.
you url should be
https://.../HQ_data_AHS/GetData?date=2022-02-02&num=6&statNum=11800
or change route
[HttpGet("~/HQ_data_AHS/GetData/{date}/{num}/{statNum}")]
public JsonResult Get(string date, int num, int statNum)
in this case you url should be
https://.../HQ_data_AHS/GetData/2022-02-02/6/11800