【发布时间】:2018-10-15 00:02:33
【问题描述】:
我正在尝试使用 asp.net web api 和 xamarin 表单开发移动应用程序。遇到这样的错误
System.Net.Http.HttpRequestException:发送时出错 请求
我在同一个解决方案中开发了所有应用程序 Mobile、Web。然后使用 Visual Studio 2017 创建具有个人登录的 Web api。 这是我的RegisterBinding模型源代码
RegisterBinding.cs
public class RegisterBindingModel
{
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
然后我为 xamarin 绑定创建了视图模型 RegisterViewModel.cs
class RegisterViewModel
{
ApiServices _apiserv = new ApiServices();
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public string Message { get; set; }
public ICommand RegisterCommand {
get
{
return new Command(async() =>
{
var isSucess = await _apiserv.RegisterAsync(Email, Password, ConfirmPassword);
if (isSucess)
{
Message = "Sucessfully Registered!";
}
else
{
Message = "Try again";
}
});
}
}
}
我还创建了 apiservice 来连接移动应用程序和 web api 服务
ApiService.cs
public async Task<bool> RegisterAsync(string email, string password, string confirmPassword)
{
try
{
var client = new HttpClient();
var model = new RegisterBindingModel
{
Email = email,
Password = password,
ConfirmPassword = confirmPassword
};
var json = JsonConvert.SerializeObject(model);
HttpContent _content = new StringContent(json);
_content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var response = await client.PostAsync("http://localhost:54996/api/Account/Register", _content);
return response.IsSuccessStatusCode;
}
catch (System.Exception)
{
throw;
}
}
最后我对 xamarin xaml 的看法是
RegisterPage.cs
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CIDSCONMOB.Views.RegisterPage"
xmlns:vm="clr-namespace:CIDSCONMOB.ViewModels">
<ContentPage.BindingContext>
<vm:RegisterViewModel/>
</ContentPage.BindingContext>
<StackLayout Orientation="Vertical">
<Entry Text="{Binding Email}"
Placeholder="Email"/>
<Entry Text="{Binding Password}"
Placeholder="Password"
IsPassword="True"/>
<Entry Text="{Binding ConfirmPassword}"
Placeholder="Confirm Password"
IsPassword="True"/>
<Button Command="{Binding RegisterCommand}"
Text="Sign Up"/>
<Label Text="{Binding Message}"/>
</StackLayout>
</ContentPage>
这是我点击注册按钮时的错误
由于我是 xamarin 开发的新手,请帮助我找到解决此问题的方法
【问题讨论】:
-
一个。不要使用本地主机,b。查看 InnerException 以获得实际的根本原因
-
System.ObjectDisposedException:无法访问已处置的对象。对象名称:“System.Net.Sockets.NetworkStream”。将 localhost 更改为 ipaddress 后
标签: c# asp.net-mvc xamarin asp.net-web-api xamarin.forms