Search code examples
c#.netxamarinroutes

Routing an API call in C# return NotFound


I have this application which connects to the API to register a User in the database. The method looks like this:

HttpClient client = new HttpClient();
                var uri = new Uri(string.Format("https://myapinameonazure.azurewebsites.net/api/register?fullName=" + username.Text + "&password=" + password.Text + "&email=" + email.Text));
                HttpResponseMessage response;
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                response = await client.GetAsync(uri);

                if(response.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    Toast.MakeText(this, "Rejestracja pomyślna.", ToastLength.Short).Show();
                }
                else
                {
                    Toast.MakeText(this, "Istnieje już konto z podanym mailem.", ToastLength.Short).Show();
                }

(credentials are declared before the method)

and the POST method in my API looks like this:

 // POST api/register
        [ActionName("Xamarin_reg")]
        [HttpPost]
        [Route("api/register")]
        [Route("api/register?")]
        [Route("api/register?fullName={fullName:string}&password={password:string}&email={email:string}")]
        HttpResponseMessage Xamarin_reg(string fullName, string password, string email)
            {
                MySqlConnection conn = new MySqlConnection(Constants.connectionString);
                MySqlDataReader reader;
                MySqlCommand checkEmailCommand = new MySqlCommand(Constants.checkIfAlreadyRegisteredQuery, conn);
                checkEmailCommand.Parameters.Add("@email", MySqlDbType.VarChar).Value = email;
                conn.Open();
                reader = checkEmailCommand.ExecuteReader();
                reader.Read();
                if (reader.HasRows)
                {
                    return new HttpResponseMessage(HttpStatusCode.Unauthorized);
                }
                else
                {
                    reader.Close();
                }
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;


                cmd.CommandText = Constants.InsertNewUserQuery;
                cmd.Parameters.Add("@username", MySqlDbType.VarChar).Value = fullName;
                cmd.Parameters.Add("@password", MySqlDbType.VarChar).Value = password;
                cmd.Parameters.Add("@password_salt", MySqlDbType.VarChar).Value = password;
                cmd.Parameters.Add("@email", MySqlDbType.VarChar).Value = email;
                cmd.Parameters.Add("@timestamp", MySqlDbType.DateTime).Value = dtNow;
                cmd.ExecuteNonQueryAsync();

                conn.Close();
                return new HttpResponseMessage(HttpStatusCode.Accepted);

            
        }

As you can see, I tried making a route template to match this URL, but it always returns StatusCode Not Found. How do I make this route work?


Solution

  • Your method only supports HttpPost so you should use this And since it's a post request, data like name, password and email should be in a body

    Make an object with username, password and email, serialize it, set headers and include it in the method

    var data = new { fullName = username.Text, password = password.Text, email = email.Text };
    var content = JsonConvert.SerializeObject(data);
    var buffer = System.Text.Encoding.UTF8.GetBytes(content);
    var byteContent = new ByteArrayContent(buffer);
    byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    var result = client.PostAsync(uri, byteContent).Result