【.Net】HttpContext.Current在执行await后为 null(HttpContext.Current null after await)

前言

.Net程序中在执行了await方法后,再次获取HttpContext.Current时,值为null,可以通过如下方法解决此问题

解决方案

主要代码:

var tempHttpContextCurrent = System.Web.HttpContext.Current;//在执行await之前,将HttpContext.Current赋值给变量

HttpResponseMessage response = await client.PutAsync("api/EditUser", content);
System.Web.HttpContext.Current = tempHttpContextCurrent;//在执行完await后,将变量赋值给HttpContext.Current

public void updateuser(){

var tempHttpContextCurrent = System.Web.HttpContext.Current;//在执行await之前,将HttpContext.Current赋值给变量

                var client = CurrentUser.CallServices.GetAPIClient();
                UserInfo userinfo = new UserInfo();
                userinfo.U_userId = 8;
                userinfo.U_description = Description;
                userinfo.U_displayName = DisplayName;
                userinfo.U_distinguishedName = sAMAccountDN;
                userinfo.U_sAMAccountName = sAMAccountName;
                userinfo.U_status = 1;

                string json = JsonConvert.SerializeObject(userinfo);
                HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
                HttpResponseMessage response = await client.PutAsync("api/EditUser", content);
                
                System.Web.HttpContext.Current = tempHttpContextCurrent;//在执行完await后,将变量赋值给HttpContext.Current

               
                if (response.IsSuccessStatusCode)
                {
                    //再次获取System.Web.HttpContext.Current时候可以正常获取到其值
                }
                else
                {
                    //再次获取System.Web.HttpContext.Current时候可以正常获取到其值
                }

}



 /// <summary>
        /// 调用webapi接口 通过httpclient
        /// </summary>
        /// <returns></returns>
        public static HttpClient GetAPIClient()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:44344/");
            // Add an Accept header for JSON format.
            // 为JSON格式添加一个Accept报头
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            return client;
        }


//Controller方法
// PUT api/values/5
        [HttpPut]
        [Route("api/EditUser")]
        public bool EditUser([System.Web.Http.FromBody] UserInfo model)
        {
            var UserInfoRepository = new UserInfoRepository();
            bool result = UserInfoRepository.Update(model);
            return result;
        }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起来学吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值