代码之家  ›  专栏  ›  技术社区  ›  Joshua Schlichting

客户端为iOS设备时无法访问HttpContext

  •  0
  • Joshua Schlichting  · 技术社区  · 4 年前

    问题是

    我遇到了一个无法访问的问题 HttpContext 在里面 @code 放置在剃须刀组件内,因为它 null 每当客户端是 网间网操作系统 移动设备 (在iOS的Safari和Firefox上测试 iPadOS。还使用Chrome在Pixel 2上进行了测试,该问题不存在)。我知道这仅限于移动iOS设备,因为从桌面浏览器(在Ubuntu上用Firefox | Chromium测试,在macOS上用Firefox |Safari测试)来看, 请求上下文 是一个包含所有预期属性的对象。

    例子

    TheComponent.razor

    @inject IHttpContextAccessor _httpContext
    
    @*html and other components that ultimately call the function below*@
    
    @code
    {
        private void SomeFuncCalledByAButton(){
        
            Console.WriteLine(_httpContext.ToString());
            //"Microsoft.AspNetCore.Http.HttpContextAccessor"
    
            if(_httpContext.HttpContext is null){
                // This block is ALWAYS entered when an iOS device visits the page and clicks the button
                Console.WriteLine("It's null!"); //It's null!
                Console.WriteLine(_httpContext.HttpContext.ToString());//Raises System.NullReferenceException
            }else{
                //This block is ALWAYS entered when DESKTOP devices access the page and click the button
                Console.WriteLine(_httpContext.HttpContext.ToString());//Microsoft.AspNetCore.Http.DefaultHttpContext
            }
        }
    
    }
    

    太长,读不下去了

    HttpContextAccessor s 请求上下文 无效的 当从iOS设备访问剃刀组件时,它是一个普通对象,但当从桌面设备访问该组件时,该组件是一个正常对象。

    我怎样才能拿到 请求上下文 什么时候 任何 浏览器访问我的剃须刀组件?

    0 回复  |  直到 4 年前