即使请求成功,HttpClient也不返回任何内容

时间:2016-12-16 07:01:13

标签: asp.net-mvc httpclient

我正在尝试从API控制器(在不同的应用程序中)调用MVC Controller操作,我可以成功调用API控制器并返回结果但是控件永远不会返回到调用方法并且PostMan显示请求仍在等待中。

我的WebApi控制器中有这个方法

 [HttpGet]
    public BaseModel GetUserId(string email)
    {
        if (string.IsNullOrWhiteSpace(email))
        {
            return new BaseModel
            {
                success = false,
                message = "invalid username or email."
            };
        }

        var result = _security.GetUserId(email);
        var baseModel = JsonConvert.DeserializeObject<BaseModel>(result.Result);

        return baseModel;
    }

_security.GetUserById

 public async Task<string> GetUserId(string userName)
    {
        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var content = new FormUrlEncodedContent(new[]
            {
            new KeyValuePair<string,string>("email",userName)
        });

        var response = await httpClient.PostAsync(baseUri + "Account/GetUserId", content);

        if (response.IsSuccessStatusCode)
        {
            var result = response.Content.ReadAsStringAsync().Result;
            return result;
        }

        return "";
    }

以下是我的API控制器操作

 [HttpPost]
    [AllowAnonymous]
    public async Task<BaseModel> GetUserId(string email)
    {
        var user = await _userManager.FindByEmailAsync(email);
        if (user == null)
        {
            return new BaseModel
            {
                success = false,
                message = "No user exists with the specified email"
            };
        }

        user = await _userManager.FindByNameAsync(email);
        if (user == null)
        {
            return new BaseModel
            {
                success = false,
                message = "No user exists with the specified username"
            };
        }

        return new BaseModel
        {
            success = true,
            data = user.Id
        };
    }

0 个答案:

没有答案