Say I have the below method
[HttpPost()]
[Route("Api/IsUserValid/{username}/{password}")]
public string ValidateUser(string username, string password)
{
var user = _context.USERS.FirstOrDefault(x => x.U_NAME.Equals(username) && x.U_PASSWORD.Equals(password));
var jsonIzedUser = JsonConvert.SerializeObject(user);
return jsonIzedUser;
}
I would like to make a post request from the xamarin project and below is my code to make the post request
using (var httpClient = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("username","ASIM"),
new KeyValuePair<string, string>("password","xxxx")
});
var response = await httpClient.PostAsync("http://10.0.2.2:44342/Api/IsUserValid", content);
string result = await response.Content.ReadAsStringAsync();
}
and Finally the error I get
StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Sun, 15 Mar 2020 17:02:46 GMT
Server: Microsoft-IIS/10.0
Transfer-Encoding: chunked
X-Android-Received-Millis: 1584291766080
X-Android-Response-Source: NETWORK 404
X-Android-Selected-Protocol: http/1.1
X-Android-Sent-Millis: 1584291765810
X-Powered-By: ASP.NET
}
What is it that I'M missing here?
Here, you declared the route parameters for username and password, which is dangerous!
[Route("Api/IsUserValid/{username}/{password}")]
public string ValidateUser(string username, string password)
At least you need to use form. So, change the code to
[Route("Api/IsUserValid")]
public string ValidateUser([FromForm] string username, [FromForm] string password)
then your code to post from the xamarin project should work without any changes