2

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?`

1

2 Answers 2

2

just fix the action

 [HttpGet("~/api/hqdataahs/GetData")]
  //or just
 [HttpGet("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://.../api/hqdataahs/GetData?date=2022-02-02&num=6&statNum=11800

or change route

 [HttpGet("~/api/hqdataahs/GetData/{date}/{num}/{statNum}")]
  //or just
 [HttpGet("GetData/{date}/{num}/{statNum}")]
[HttpGet("~api/hqdataahs/GetData
 public JsonResult Get(string date, int num, int statNum)

in this case you url should be

https://.../api/hqdataahs/GetData/2022-02-02/6/11800
Sign up to request clarification or add additional context in comments.

4 Comments

I try with your method but my route is [Route("api/hqdataahs")], how to access the API page ?
@BenJohnson Just don forget that route should start with "~/" [Route("~/.. and you can select for the route any words that you like, it should be the same for url
How to be api/hqdataahs/GetData?date=2022-02-02&num=6&staNum=11800 ?
Can you change your answer like that, please
1

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});"

3 Comments

How to call after that ?
@BenJohnson see serge answer
Ok but my route is different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.