Web Api 在 Windows 10 上可以 100% 运行,但是一旦我在 Windows 11 机器上运行它就无法运行。
我得到的错误。 System.Data.SqlClient.SqlException (0x80131904):参数化查询“(@UserName nvarchar(4000))SELECT * FROM User_Table WHERE @UserNa”需要未提供的参数“@UserName”。
Asp运行.net6.0。当相同的 web api 在 win 10 机器上运行时,它流畅快速,没有问题。 Windows 11 是最新的我在写这篇文章之前尝试更新它,所以这不是问题。这篇文章是我最后的选择,因为我在网上找不到同样的问题。我正在使用 Postman 对其进行测试。
守则
[HttpPost]
public IActionResult post([FromBody] User user)
{
string query = "INSERT INTO User_Table (ut_User, ut_UserCode, ut_Branch) VALUES (@UserName, @UserCode, @Branch)";
string query2 = "SELECT * FROM User_Table WHERE @UserName = ut_User";
string sqlDataSource = _configuration.GetConnectionString("IMSConnStr");
using (SqlConnection mycon = new SqlConnection(sqlDataSource))
{
mycon.Open();
using (SqlCommand cmd = new SqlCommand(query2, mycon))
{
cmd.Parameters.AddWithValue("@UserName", user.UserName);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows) return Ok(200);
}
}
using (SqlConnection mycon = new SqlConnection(sqlDataSource))
{
mycon.Open();
using (SqlCommand cmd = new SqlCommand(query, mycon))
{
cmd.Parameters.AddWithValue("@UserName", user.UserName);
cmd.Parameters.AddWithValue("@UserCode", user.UserCode);
cmd.Parameters.AddWithValue("@Branch", user.Branch);
cmd.ExecuteNonQuery();
}
mycon.Close();
}
return Ok(200);
}
}
我曾尝试将 FromBody 更改为 FromForm,但随后出现 415 Unsupported Media Type 错误。我还尝试专门将 Content-Type 设置为 application/json,这并没有改变任何东西。