Hello,
as the title says 'Blazor Server-Side on Azure has null HttpContext'
it works perfectly fine on local IIS but on azure it fails
why and how to fix this ?
i need to be able to inject HttpContext to scope service so i can get client info such as IP
Thanks :)
AspNetCore 3.0
@mRmEEeZ Thanks for contacting us.
We don't recommend relying on HTTP Context in server-side Blazor applications. This is due to the fact that the HttpContext is not generally available in the underlying signalr connection.
As such you should avoid using it for anything. Instead, the recommendation is to capture anything you want during prerendering and pass it as a parameter to the root component of your app.
@javiercn
You did not give me any solution sir.
i need to get the Context of the connected client to access his IP
can i at least get example of this statement of yours
the recommendation is to capture anything you want during prerendering and pass it as a parameter to the root component of your app
Here is a sample on how to do it by capturing the information and passing it as a parameter.
@javiercn
Thanks for your reply
i tried your code within mine , it didn't work , sadly
i tried this line of code in _Host.cshtml
<component type="typeof(App)" render-mode="ServerPrerendered" param-IP="@HttpContext.Connection.RemoteIpAddress.ToString()" />
only white screen...
i tried this..
<app>@(await Html.RenderComponentAsync<App>(RenderMode.Server,new { IP = HttpContext.Connection.RemoteIpAddress.ToString() }))</app>
it threw
InvalidOperationException: Server components with parameters are not supported.
of course tried with cascading parameter as shown in your project file
this is my App.razor
@using SystemCore.LayoutComponents;
@using Microsoft.AspNetCore.Components.Web;
@using FacilityManagementModule.ViewModels;
@using IdentityManagementModule.ViewModels;
@inject SystemCore.Interfaces.IAppState appState;
<CascadingValue Name="IP" Value="@IP">
<Router AppAssembly="@typeof(Startup).Assembly"
AdditionalAssemblies="new List<System.Reflection.Assembly>() {
typeof(HelpDeskViewModel).Assembly,typeof(LayoutComponent).Assembly,
typeof(ProfileViewModel).Assembly }">
<Found Context="routeData">
<RouteView RouteData="@routeData" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(TheSystem.Shared.Index)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingValue>
@code{
[Parameter] public string IP { get; set; }
protected override void OnInitialized()
{
appState.IP = IP;
}
}
i hope you can figure it out as this is so essential in my app
BTW your project didn't work as there is missing files
@javiercn
sorry mate , i re-run your files in VS preview and it worked
it is because aspnetcore 3.1
can you show me how to do it in aspnetcore 3.0 ?
thanks
@mRmEEeZ It's a bit more involved to do in 3.0. You could use a middleware in the pipeline to capture that information and add it to the current principal, or something similar.
It is much better on 3.1, (as per the sample) so I suggest you simply update to the latest 3.1 release that which has a go-live license. See the blog post here https://devblogs.microsoft.com/dotnet/announcing-net-core-3-1-preview-3/
@javiercn
Thanks brother , i tried it and worked
+1
Most helpful comment
@mRmEEeZ It's a bit more involved to do in 3.0. You could use a middleware in the pipeline to capture that information and add it to the current principal, or something similar.
It is much better on 3.1, (as per the sample) so I suggest you simply update to the latest 3.1 release that which has a go-live license. See the blog post here https://devblogs.microsoft.com/dotnet/announcing-net-core-3-1-preview-3/