Home > database >  Web API - Post Error - {Message: 'Value cannot be null.\r\nParameter name: uriString'}
Web API - Post Error - {Message: 'Value cannot be null.\r\nParameter name: uriString'}

Time:02-22

I have a problem with my POST on ASP.NET . Basically the when I try to POST a new User, I get the status: "400 Bad Request" on Postman, but the User is being created! The error message is: "Value cannot be null.\r\nParameter name: uriString" . Can someone tell my why it happenes?

My code is:

Controller:

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

Models (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;
}

(I'm using SQL)

Thanks for all the helpers

Updated (the added code of "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;
        }

CodePudding user response:

if it is inside of the same controller try to replace the line

return Created(new Uri(Url.Link("GetUserByID", new { user_id = res })), User2Insert);

that causes a problem, with this line

return Get(res);

or if it is in a different controller , you can try this

return RedirectToRoute("GetUserByID", new { userId= res });
  • Related