asp.net WebAPI 调用不适用于 Xamarin.Forms [英] asp.net WebAPI call not working from Xamarin.Forms
问题描述
在我将 Id 的类型从 Int 更改为字符串后,我的 Xamarin 不再调用我的 api.但这不能成为原因,在此之前它可以正常工作
My Xamarin dosnt call anymore my api after i change the typ of Id from Int to string. But that can't be the reason, before that it works without any problems
当我使用 Postman 测试端点时,一切正常
When I test the endpoint with Postman, everything works fine
当我调试它时,应用程序一直运行到 API 调用中的响应,然后中断而不显示错误消息.也不例外.
When I debug it, the application runs through to response in the API call and then breaks off without an error message. No exception.
API 调用
public async Task<bool> LoginUser(string username, string password)
{
_acc.UserName = username;
_acc.Password = password;
var httpClient = new HttpClient();
var json = JsonConvert.SerializeObject(_acc);
var content = new StringContent(json, Encoding.UTF8, "application/json");
**var response = await httpClient.PostAsync("http://192.168.178.41:45472/api/account/Login", content);**
//这里是中断我的 Web API 调用.
//here is the break my Web API dosnt call.
if (!response.IsSuccessStatusCode)
{
return false;
}
return true;
}
我的 DI:
public async Task LoginUser(string username, string password)
{
var result = await _accService.LoginUser(username, password);
if (result)
{
await _navService.NavigateToAsync<Profile_ViewModel>();
}
else
{
await Application.Current.MainPage.DisplayAlert("nope", "nope", "nope");
}
}
我在 ViewModel 中的方法
My method in the ViewModel
private async void GoLogin()
{
var container = ContainerConfig.CreateContainer();
using (var scope = container.BeginLifetimeScope())
{
var GetService = scope.Resolve<IMyUser_Service>();
await GetService.LoginUser(UserName, Password);
}
}
推荐答案
大家好,我解决了我的问题.我的模型设置不正确.
Hi guys i solved my problem. My Model wasnt set right.
这篇关于asp.net WebAPI 调用不适用于 Xamarin.Forms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!