简体   繁体   English

asp.net WebAPI 调用无法从 Xamarin.Forms

[英]asp.net WebAPI call not working from Xamarin.Forms

My Xamarin dosnt call anymore my api after i change the typ of Id from Int to string.在我将 Id 的类型从 Int 更改为字符串后,我的 Xamarin 不再调用我的 api。 But that can't be the reason, before that it works without any problems但这不可能是原因,在此之前它可以正常工作

When I test the endpoint with Postman, everything works fine当我用 Postman 测试端点时,一切正常

When I debug it, the application runs through to response in the API call and then breaks off without an error message.当我调试它时,应用程序运行到 API 调用中的响应,然后中断而没有错误消息。 No exception.没有例外。

API Call 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);**

//here is the break my Web API dosnt call. //这是中断我的 Web API dosnt 调用。

        if (!response.IsSuccessStatusCode)
        {
            return false;
        }

        return true;

    }

My DI:我的 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");
        }
    }

My method in the ViewModel我在 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);
        }

    }

Xamarin.Droid: The AccessNetworkState permission is required. Xamarin.Droid:需要 AccessNetworkState 权限。

Open the AssemblyInfo.cs file under the properties folder and add this:打开属性文件夹下的 AssemblyInfo.cs 文件并添加:

[assembly: UsesPermission(Android.Manifest.Permission.AccessNetworkState)]

Or update the Android Manifest file.或者更新 Android 清单文件。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在此处输入图像描述

Xamarin.iOS: App Transport Security enforces secure connections between the app's back-end server and your app. Xamarin.iOS:应用程序传输安全性在应用程序的后端服务器和您的应用程序之间实施安全连接。 You can add the following code to your app's Info.plist file and It will disable the security defaults that ATS enforces for a given domain:您可以将以下代码添加到应用程序的 Info.plist 文件中,它将禁用 ATS 对给定域强制执行的安全默认值:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>192.168.178.41:45472</key> <!-- Your Domain address -->
        <dict>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

Or you can make the following changes to your app's Info.plist file to completely disable ATS for all domains and internet communication:或者,您可以对应用的 Info.plist 文件进行以下更改,以完全禁用所有域和 Internet 通信的 ATS:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

Hi thanks for the help until now.嗨,感谢您到现在为止的帮助。 I make some checks and now i can say its my localhost what is broken.我做了一些检查,现在我可以说我的本地主机坏了。 i get only https from conveyor but i need the http version because my app throw me the handshakeexception if i use https.我从传送带上只得到 https,但我需要 http 版本,因为如果我使用 https,我的应用程序会抛出握手异常。

I try it with azure and now there throw me "System.ObjectDisposedException: 'Cannot access a closed Stream.'" its the same code i only remove the using statement at GoLogin and replace the endpoint to my azure web service. I try it with azure and now there throw me "System.ObjectDisposedException: 'Cannot access a closed Stream.'" its the same code i only remove the using statement at GoLogin and replace the endpoint to my azure web service.

Still in postman works fine..仍在 postman 工作正常..

I am happy about help我很高兴得到帮助

Hi guys i solved my problem.大家好,我解决了我的问题。 My Model wasnt set right.我的 Model 设置不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM