1

How can we call the following web api ?

[HttpPost]
public bool ValidateAdmin(string username, string password)
{
    return _userBusinessObject.ValidateAdmin(username, password);
}

I've written the following code, but it dosen't work 404 (Not Found)

string url = string.Format("api/User/ValidateAdmin?password={0}", password);
HttpResponseMessage response = Client.PostAsJsonAsync(url, username).Result;
response.EnsureSuccessStatusCode();
return response.Content.ReadAsAsync<bool>().Result;

Edit:
I'm dead sure about the Url, but it says 404 (Not Found)

1 Answer 1

2

i do like this for me in similar case :

MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
HttpContent datas = new ObjectContent<dynamic>(new { 
    username= username, 
    password= password}, jsonFormatter);

var client = new HttpClient();

client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));

var response = client.PostAsync("api/User/ValidateAdmin", datas).Result;

if (response != null)
{
    try
    {
        response.EnsureSuccessStatusCode();
        return response.Content.ReadAsAsync<bool>().Result;

        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.