I am trying to invoke get method in web api which will take 2 parameters from client side. Here User will enter username & password & it will be sent to server for authentication.
This is my controller Vendor (which I have created in mvc) & its web api method
[Route("api/Vendor/{uname}/{pass}")]
public int Get(string uname, string pass)
{
Boolean exists = false;
int id = 0;
SqlConnection con = new SqlConnection(" Data Source = DELL; Initial Catalog = EVENT; Integrated Security = True");
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Count([UserName]) As Usercount FROM [dbo].[AllUser] WHERE [UserName] = '" + uname + "' AND [Password] = '" + pass + "' ", con))
{
exists = (int)cmd.ExecuteScalar() > 0;
}
if (exists)
{
SqlCommand cmd = new SqlCommand("SELECT [Id] FROM [dbo].[AllUser] WHERE [UserName] = '" + uname + "' AND [Password] = '" + pass + "' ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
id = Convert.ToInt16(ds.Tables[0].Rows[0][0]);
}
return id;
}
Now I am stuck as how to send 2 parameters to this api. Do I have to convert them into JSON or can I simply send them directly. I don't know how to call this get mehod.
I am using c# and android at client side. Please, give me suggestions on how to invoke this method from both side.
Thanks.
Actually i updated my c# client side with the following code
static async Task<int> ValidateVendorAsync(string uname, string pass)
{
int id = 0;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:56908/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
HttpResponseMessage response = await client.GetAsync("api/Vendor/" + uname + "/" + pass);
if (response.IsSuccessStatusCode)
{
System.Diagnostics.Debug.WriteLine("Con");
}
else
{
System.Diagnostics.Debug.WriteLine("Connection Error");
}
return id;
}
}
But the code doesn't work. The code after await client.GetAsync is not executing. How to solve this?