如何使用 ASP.NET Core Web API 将三个参数传递给 url 地址
How to pass three parameters to url address using ASP.NET Core Web API
现在我有一个我想调用的程序:
CALL Get_Discharge_Station('2022-02-02', 6, 11800);
我想把这三个参数-2022-02-02,6,11800
传递给url地址,当我输入不同的日期,号码和站号时得到不同的数据。
现在我的 API 控制器看起来像这样:
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);
}
}
}
当我访问控制器时,我可以更改什么以在 url 地址中传递三个参数?`
声明参数
[HttpGet]
public JsonResult Get(string dataStr, int noodle, int frumpy)
{
现在构建调用
string query = $"CALL Get_Discharge_Station('{dateStr}', {noodle}, {fumpy});"
只需修正动作
[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});";
.....
并仔细阅读下面所有关于 sql 注入的评论。
你url应该
https://.../api/hqdataahs/GetData?date=2022-02-02&num=6&statNum=11800
或更改路线
[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)
在这种情况下,你 url 应该是
https://.../api/hqdataahs/GetData/2022-02-02/6/11800
现在我有一个我想调用的程序:
CALL Get_Discharge_Station('2022-02-02', 6, 11800);
我想把这三个参数-2022-02-02,6,11800
传递给url地址,当我输入不同的日期,号码和站号时得到不同的数据。
现在我的 API 控制器看起来像这样:
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);
}
}
}
当我访问控制器时,我可以更改什么以在 url 地址中传递三个参数?`
声明参数
[HttpGet]
public JsonResult Get(string dataStr, int noodle, int frumpy)
{
现在构建调用
string query = $"CALL Get_Discharge_Station('{dateStr}', {noodle}, {fumpy});"
只需修正动作
[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});";
.....
并仔细阅读下面所有关于 sql 注入的评论。
你url应该
https://.../api/hqdataahs/GetData?date=2022-02-02&num=6&statNum=11800
或更改路线
[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)
在这种情况下,你 url 应该是
https://.../api/hqdataahs/GetData/2022-02-02/6/11800