So 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 return StatusCode Not Found. How do I make this route work?