Web API - Post 错误 - {消息:'Value cannot be null.\r\nParameter name: uriString'}
Web API - Post Error - {Message: 'Value cannot be null.\r\nParameter name: uriString'}
我在 ASP.NET 上的 POST 有问题。
基本上,当我尝试 POST 一个新用户时,我在 Postman 上得到状态:“400 Bad Request”,但正在创建用户!
错误消息是:“值不能为空。\r\nParameter 名称:uriString”。
谁能告诉我为什么会这样?
我的代码是:
控制器:
// POST CREATE NEW USER
public IHttpActionResult Post([FromBody] Users User2Insert)
{
try
{
int res = _usersDB.InsertUserToDb(User2Insert);
if (res == -1)
{
return Content(HttpStatusCode.BadRequest, $"User id = {User2Insert.User_ID} was not created in the DB!!!");
}
User2Insert.User_ID = res;
return Created(new Uri(Url.Link("GetUserByID", new { user_id = res })), User2Insert);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
型号 (Class) :
//INSERT USER
public int InsertUserToDb(Users val)
{
//in case thers already a user with such email
if (GetUserByEmail(val.Email) != null) return -1;
string strComm =
$" INSERT INTO Users(first_name, last_name, email, pass) VALUES(" +
$" N'{val.First_Name}'," +
$" N'{val.Last_Name}'," +
$" N'{val.Email}'," +
$" N'{val.Pass}'); ";
strComm +=
" SELECT SCOPE_IDENTITY() AS[SCOPE_IDENTITY]; ";
return ExcReaderInsertUser(strComm);
}
public int ExcReaderInsertUser(string comm2Run)
{
int UserID = -1;
SqlConnection con = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(comm2Run, con);
comm.Connection.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
UserID = int.Parse(reader["SCOPE_IDENTITY"].ToString());
}
comm.Connection.Close();
return UserID;
}
(我正在使用 SQL)
感谢大家的帮助
已更新(“GetUserByID”新增代码:
[Route("{id:int:min(1)}", Name = "GetUserByID")]
public IHttpActionResult Get(int user_id)
{
try
{
Users u = _usersDB.GetUserByID(user_id);
if (u != null)
{
return Ok(u);
}
return Content(HttpStatusCode.NotFound, $"User with id {user_id} was not found!!!");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
public Users GetUserByID(int user_id)
{
Users u = null;
SqlConnection con = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(
$" SELECT * FROM Users " +
$" WHERE user_id='{user_id}'", con);
comm.Connection.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
u = new Users(
(int)reader["user_id"],
(string)reader["first_name"],
(string)reader["last_name"],
(string)reader["email"],
(string)reader["pass"]
);
}
comm.Connection.Close();
return u;
}
如果它在同一个控制器中,请尝试更换行
return Created(new Uri(Url.Link("GetUserByID", new { user_id = res })), User2Insert);
导致问题,这一行
return Get(res);
或者如果它在不同的控制器中,你可以试试这个
return RedirectToRoute("GetUserByID", new { userId= res });
我在 ASP.NET 上的 POST 有问题。 基本上,当我尝试 POST 一个新用户时,我在 Postman 上得到状态:“400 Bad Request”,但正在创建用户! 错误消息是:“值不能为空。\r\nParameter 名称:uriString”。 谁能告诉我为什么会这样?
我的代码是:
控制器:
// POST CREATE NEW USER
public IHttpActionResult Post([FromBody] Users User2Insert)
{
try
{
int res = _usersDB.InsertUserToDb(User2Insert);
if (res == -1)
{
return Content(HttpStatusCode.BadRequest, $"User id = {User2Insert.User_ID} was not created in the DB!!!");
}
User2Insert.User_ID = res;
return Created(new Uri(Url.Link("GetUserByID", new { user_id = res })), User2Insert);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
型号 (Class) :
//INSERT USER
public int InsertUserToDb(Users val)
{
//in case thers already a user with such email
if (GetUserByEmail(val.Email) != null) return -1;
string strComm =
$" INSERT INTO Users(first_name, last_name, email, pass) VALUES(" +
$" N'{val.First_Name}'," +
$" N'{val.Last_Name}'," +
$" N'{val.Email}'," +
$" N'{val.Pass}'); ";
strComm +=
" SELECT SCOPE_IDENTITY() AS[SCOPE_IDENTITY]; ";
return ExcReaderInsertUser(strComm);
}
public int ExcReaderInsertUser(string comm2Run)
{
int UserID = -1;
SqlConnection con = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(comm2Run, con);
comm.Connection.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
UserID = int.Parse(reader["SCOPE_IDENTITY"].ToString());
}
comm.Connection.Close();
return UserID;
}
(我正在使用 SQL)
感谢大家的帮助
已更新(“GetUserByID”新增代码:
[Route("{id:int:min(1)}", Name = "GetUserByID")]
public IHttpActionResult Get(int user_id)
{
try
{
Users u = _usersDB.GetUserByID(user_id);
if (u != null)
{
return Ok(u);
}
return Content(HttpStatusCode.NotFound, $"User with id {user_id} was not found!!!");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
public Users GetUserByID(int user_id)
{
Users u = null;
SqlConnection con = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(
$" SELECT * FROM Users " +
$" WHERE user_id='{user_id}'", con);
comm.Connection.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
u = new Users(
(int)reader["user_id"],
(string)reader["first_name"],
(string)reader["last_name"],
(string)reader["email"],
(string)reader["pass"]
);
}
comm.Connection.Close();
return u;
}
如果它在同一个控制器中,请尝试更换行
return Created(new Uri(Url.Link("GetUserByID", new { user_id = res })), User2Insert);
导致问题,这一行
return Get(res);
或者如果它在不同的控制器中,你可以试试这个
return RedirectToRoute("GetUserByID", new { userId= res });