'Call get method in web api with multiple parameters from c#

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?



Solution 1:[1]

You have to pass the parameters into the query string as below,

http://localhost:49996/api/Vendor?uname=CharanGhate&pass=xyz

http://localhost:49996/ is the base url of your application. You have to use the http get method here. You can call the get type method directly from browser as well and also from postman, fiddler etc

Solution 2:[2]

You have everything setup correctly, you just need to make an HTTP GET request to that url: http://baseurl/api/Vendor/myusername/mypassword

Parameters passed in the route do not need to be serialized. If they are not dedclared in the route, then you have to pass a serialized version. In this case they would be appended as part of the querystring.

So going with your HttpClient example:

var client = new HttpClient() //if used frequently, don't dispose this guy, make him a singleton if you can (see link below)

      String uname = UserName.Text;
    String pass = Password.Text;

    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");

     var result = await client.GetAsync("http://localhost:56908/api/vendor/" + uname + "/" + pass);

https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Charan Ghate
Solution 2