【问题标题】:Flurl Post Not Returning from Web ApiFlurl Post 未从 Web Api 返回
【发布时间】:2016-11-09 12:29:00
【问题描述】:

我有一个使用 Flurl 的 Xamarin 应用程序,下面是 Web Api 的帖子

Xamarin 应用程序:

    private async Task<LoginResponse> processLogin()
    {
        try
        {

            return await "http://192.168.0.12:60257/api/loginapi/Login".WithTimeout(10).PostJsonAsync(new { username = "fsdafsd", password = "gdfgdsf" }).ReceiveJson<LoginResponse>();
        }
        catch (Exception e)
        {
            return new LoginResponse { ResponseStatusCode = -1 };
        }
    }

网页接口:

public LoginResponse Login([FromBody]LoginRequest loginRequest)
{
    var result = new LoginResponse();

    try
    {
        var user = this.UserManager.FindAsync(loginRequest.username, loginRequest.password);

        if (user != null)
        {
            result.ResponseStatusCode = 1;
        }
        else
        {
            result.ResponseStatusCode = 0;
        }

    }
    catch (Exception e)
    {

        result.ResponseStatusCode = -1;
    }

    return result;

}

我可以看到我的 Web Api 方法被命中,它返回预期的对象类型,而不是我的 Xamarin 应用程序继续等待 Flurl Post。

谁能告诉我可能做错了什么?

更新:

我注意到以下方法确实有效,但并不理想:

动态结果 = await "http://192.168.0.12:60257/api/loginapi/Login".PostJsonAsync(new { username = "fsdafsd", password = "gdfgdsf" }).ReceiveJson();

【问题讨论】:

  • 请贴出 LoginResponse 的完整类定义

标签: api web xamarin flurl


【解决方案1】:

修复它。无论出于何种原因,这是我试图返回的类型。将对象变量类型更改为“动态”解决了这个问题,并允许我正确反序列化对象。

动态结果 = await "http://192.168.0.12:60257/api/loginapi/Login".PostJsonAsync(new { username = "fsdafsd", password = "gdfgdsf" }).ReceiveJson();

返回一个动态对象,该对象具有我在正常结构中所期望的属性。

如果有人能启发我为什么我做不到:

登录请求结果 = ...

不胜感激。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2018-10-04
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    相关资源
    最近更新 更多