Postgres .NET core 3.1 web api, npgsql, 无法使用 POST 方法
Postgres .NET core 3.1 web api, npgsql, cannot use POST method
我 运行 遇到了问题。我有 .net core 3.1 web api 可以成功地使用 GET 方法从 postgres 本地主机获取参与者 table 数据,但我不能使用 POST 方法。 Postman 抛出错误““参数‘@FirstName’必须设置其值””,尽管给它非空输入。我不确定问题是我没有正确使用邮递员还是 C# 代码错误。我是 .NET core 3.1 的新手,所以非常欢迎任何帮助。
参加者table:
-- Table: public.Participant
-- DROP TABLE IF EXISTS public."Participant";
CREATE TABLE IF NOT EXISTS public."Participant"
(
"Id" integer NOT NULL,
"First Name" character varying COLLATE pg_catalog."default" NOT NULL,
"Last Name" character varying COLLATE pg_catalog."default" NOT NULL,
"Date Of Birth" date,
"Sex" character varying COLLATE pg_catalog."default",
CONSTRAINT "Participant_pkey" PRIMARY KEY ("First Name", "Last Name"),
CONSTRAINT "Id" UNIQUE ("Id")
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public."Participant"
OWNER to postgres;
Post方法:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Npgsql;
using Assignment.Models;
using System;
using System.Data;
namespace Assignment.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ParticipantController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;
public ParticipantController(IConfiguration configuration, IWebHostEnvironment env)
{
_configuration = configuration;
_env = env;
}
[HttpGet]
public JsonResult Get()
{
string query = @"
select * from ""Participant""
";
try
{
//create datatable and configuration stirng
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("ParticipantConnectionString");
//initialize npgsql reader
NpgsqlDataReader myReader;
using (NpgsqlConnection myCon = new NpgsqlConnection(sqlDataSource))
{
myCon.Open();
//create command from given query and connection
using (NpgsqlCommand myCommand = new NpgsqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
myCon.Close();
}
}
return new JsonResult(table);
}
catch (Exception e)
{
return new JsonResult(e);
}
}
[HttpPost]
public JsonResult Post(Participant participant)
{
string query = @"
insert into ""Participant""
(""Id"", ""First Name"", ""Last Name"", ""Date Of Birth"", ""Sex"")
values (@Id,@FirstName,@LastName,@DateOfBirth,@Sex)
";
//create datatable and configuration stirng
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("ParticipantConnectionString");
//initialize npgsql reader
NpgsqlDataReader myReader;
try
{
using (NpgsqlConnection myCon = new NpgsqlConnection(sqlDataSource))
{
myCon.Open();
using (NpgsqlCommand myCommand = new NpgsqlCommand(query, myCon))
{
myCommand.Parameters.AddWithValue("@Id", participant.Id);
myCommand.Parameters.AddWithValue("@FirstName", participant.FirstName);
myCommand.Parameters.AddWithValue("@LastName", participant.LastName);
myCommand.Parameters.AddWithValue("@DateOfBirth", Convert.ToDateTime(participant.DateOfBirth));
myCommand.Parameters.AddWithValue("@Sex", participant.Sex);
//myCommand.Parameters.AddWithValue("@PhotoFileName", participant.PhotoFileName);
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Added Successfully");
}
catch (Exception e)
{
return new JsonResult(e);
}
}
}
}
参加者class:
public class Participant
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DateOfBirth { get; set; }
public string Sex { get; set; }
}
您正在发送 "First Name"
(带有 space),但您的模型包含 "FirstName"
(没有 space)
您可以修改您 post 的内容(删除 space),或者指示您的模型接受对包含 space 的参数的绑定(请参阅 doc)
[FromQuery(Name = "First Name")]
public string FirstName { get; set; }
我 运行 遇到了问题。我有 .net core 3.1 web api 可以成功地使用 GET 方法从 postgres 本地主机获取参与者 table 数据,但我不能使用 POST 方法。 Postman 抛出错误““参数‘@FirstName’必须设置其值””,尽管给它非空输入。我不确定问题是我没有正确使用邮递员还是 C# 代码错误。我是 .NET core 3.1 的新手,所以非常欢迎任何帮助。
参加者table:
-- Table: public.Participant
-- DROP TABLE IF EXISTS public."Participant";
CREATE TABLE IF NOT EXISTS public."Participant"
(
"Id" integer NOT NULL,
"First Name" character varying COLLATE pg_catalog."default" NOT NULL,
"Last Name" character varying COLLATE pg_catalog."default" NOT NULL,
"Date Of Birth" date,
"Sex" character varying COLLATE pg_catalog."default",
CONSTRAINT "Participant_pkey" PRIMARY KEY ("First Name", "Last Name"),
CONSTRAINT "Id" UNIQUE ("Id")
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public."Participant"
OWNER to postgres;
Post方法:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Npgsql;
using Assignment.Models;
using System;
using System.Data;
namespace Assignment.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ParticipantController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;
public ParticipantController(IConfiguration configuration, IWebHostEnvironment env)
{
_configuration = configuration;
_env = env;
}
[HttpGet]
public JsonResult Get()
{
string query = @"
select * from ""Participant""
";
try
{
//create datatable and configuration stirng
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("ParticipantConnectionString");
//initialize npgsql reader
NpgsqlDataReader myReader;
using (NpgsqlConnection myCon = new NpgsqlConnection(sqlDataSource))
{
myCon.Open();
//create command from given query and connection
using (NpgsqlCommand myCommand = new NpgsqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
myCon.Close();
}
}
return new JsonResult(table);
}
catch (Exception e)
{
return new JsonResult(e);
}
}
[HttpPost]
public JsonResult Post(Participant participant)
{
string query = @"
insert into ""Participant""
(""Id"", ""First Name"", ""Last Name"", ""Date Of Birth"", ""Sex"")
values (@Id,@FirstName,@LastName,@DateOfBirth,@Sex)
";
//create datatable and configuration stirng
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("ParticipantConnectionString");
//initialize npgsql reader
NpgsqlDataReader myReader;
try
{
using (NpgsqlConnection myCon = new NpgsqlConnection(sqlDataSource))
{
myCon.Open();
using (NpgsqlCommand myCommand = new NpgsqlCommand(query, myCon))
{
myCommand.Parameters.AddWithValue("@Id", participant.Id);
myCommand.Parameters.AddWithValue("@FirstName", participant.FirstName);
myCommand.Parameters.AddWithValue("@LastName", participant.LastName);
myCommand.Parameters.AddWithValue("@DateOfBirth", Convert.ToDateTime(participant.DateOfBirth));
myCommand.Parameters.AddWithValue("@Sex", participant.Sex);
//myCommand.Parameters.AddWithValue("@PhotoFileName", participant.PhotoFileName);
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
myCon.Close();
}
}
return new JsonResult("Added Successfully");
}
catch (Exception e)
{
return new JsonResult(e);
}
}
}
}
参加者class:
public class Participant
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DateOfBirth { get; set; }
public string Sex { get; set; }
}
您正在发送 "First Name"
(带有 space),但您的模型包含 "FirstName"
(没有 space)
您可以修改您 post 的内容(删除 space),或者指示您的模型接受对包含 space 的参数的绑定(请参阅 doc)
[FromQuery(Name = "First Name")]
public string FirstName { get; set; }