说我有以下方法
[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;
}
我想从xamarin项目中发出发帖请求,下面是我发出发帖请求的代码
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();
}
最后我得到的错误
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
}
我在这里想念什么?
答案 0 :(得分:0)
在这里,您声明了用户名和密码的路由参数,这很危险!
[Route("Api/IsUserValid/{username}/{password}")]
public string ValidateUser(string username, string password)
至少您需要使用表格。因此,将代码更改为
[Route("Api/IsUserValid")]
public string ValidateUser([FromForm] string username, [FromForm] string password)
然后您从xamarin项目发布的代码应该可以正常工作