The problem
I'm running into an issue where I cannot access HttpContext
in @code
placed within a razor component because it is null
whenever the client is an iOS mobile device(tested on Safari and Firefox with iOS and iPadOS. Also tested on a Pixel 2 using Chrome, and the issue was non-existent). I know this is limited to mobile iOS devices because, from a desktop browser (tested with Firefox|Chromium on Ubuntu, and Firefox|Safari on macOS), HttpContext
is an object containing all of the expected attributes.
Example
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
}
}
}
tl;dr
HttpContextAccessor
's HttpContext
is null
when the razor component is accessed from an iOS device, but is a normal object when the component is accessed from a desktop device.
How can I go about getting the HttpContext
when any browser accesses my razor component?