How do I access HttpContext in Server-side Blazor?

Alexander Christov

I need to access HttpContext in a page (.cshtml) and in particular a request and then a cookie. Despite available, HttpContextAccessor always has a null value stored in its HttpContext property.

Any ideas would be much appreciated.

Thanks in advance.

EDIT: the Blazor version I use is: 0.7.0.

enet

Add the following to Blazor.Web.App.Startup.cs:

services.AddHttpContextAccessor();

You also need this in <component-name>.cshtml

@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor httpContextAccessor

Note: At the time when this answer was written, accessing the HttpContext was done as described above. Since then, Blazor has been under rapid development, and has fundamentally changed. It is definitely deprecated the usage described above, but alas, you can still do the above, which is legitimate and right, if you access the HttpContext from a .cshtml page. This has not changed... Thus the only place from which you can access the HttpContext, without even adding the IHttpContextAccessor to the DI container, is the _Host.cshtml file, which is a Razor Pages file, with the .cshtml extension. When the code in this file is executed, Blazor is still not born, and the execution of this file will be serving the Blazor Server App. Please, see this answer as to how to do it properly...

Hope this helps...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I access the SSH client side from the SSH server side?

From Dev

How do I access HttpContext.Current.Session in a class library?

From Dev

Glimpse - How do I access HttpContext.Session inside an IRuntimePolicy

From Dev

How do I know if a lib is server side, client side or both?

From Dev

How do I know if a lib is server side, client side or both?

From Dev

How do I access role objects from HttpContext, or more specifically from a custom authorize attribute?

From Dev

How do I create a new ApiController with HttpContext?

From Dev

How do I know if a method was called server-side or client-side (from the server)?

From Dev

How do I get the server-side exception message with GWT?

From Dev

How do I create a new server side view with Treeline?

From Dev

How do I get text of label when changed to server side?

From Dev

How do I receive data on the server side of an SSL connection?

From Dev

How can i do server side paginations in jsgrid?

From Dev

How do I get text of label when changed to server side?

From Dev

How do I stop server side event getting called

From Dev

How do I receive data on the server side of an SSL connection?

From Dev

In Django, how do I perform server-side functions and monitor progress on client side

From Dev

In Meteor, how do I know on the client-side when the server-side operation is done?

From Dev

In Meteor, how do I know on the client-side when the server-side operation is done?

From Java

How to Localize validation message (DataAnnotationsValidator) in blazor server side

From Dev

when I don't have access to the Server how can i close the socket Connection from client side?

From Dev

how do I access the object the object i sent to the server file

From Dev

how do I access the object the object i sent to the server file

From Dev

How to do unit test for HttpContext.Current.Server.MapPath

From Dev

How to do unit test for HttpContext.Current.Server.MapPath

From Dev

How do I get fake path for HttpContext.Current.Server.MapPath which is assigned to protected object inside method unit testing?

From Dev

How do I get fake path for HttpContext.Current.Server.MapPath which is assigned to protected object inside method unit testing?

From Dev

How can I access server-side variables via Meteor.call()?

From Dev

How to navigate to a blazor component from a razor page in a Blazor server-side app?

Related Related

  1. 1

    How do I access the SSH client side from the SSH server side?

  2. 2

    How do I access HttpContext.Current.Session in a class library?

  3. 3

    Glimpse - How do I access HttpContext.Session inside an IRuntimePolicy

  4. 4

    How do I know if a lib is server side, client side or both?

  5. 5

    How do I know if a lib is server side, client side or both?

  6. 6

    How do I access role objects from HttpContext, or more specifically from a custom authorize attribute?

  7. 7

    How do I create a new ApiController with HttpContext?

  8. 8

    How do I know if a method was called server-side or client-side (from the server)?

  9. 9

    How do I get the server-side exception message with GWT?

  10. 10

    How do I create a new server side view with Treeline?

  11. 11

    How do I get text of label when changed to server side?

  12. 12

    How do I receive data on the server side of an SSL connection?

  13. 13

    How can i do server side paginations in jsgrid?

  14. 14

    How do I get text of label when changed to server side?

  15. 15

    How do I stop server side event getting called

  16. 16

    How do I receive data on the server side of an SSL connection?

  17. 17

    In Django, how do I perform server-side functions and monitor progress on client side

  18. 18

    In Meteor, how do I know on the client-side when the server-side operation is done?

  19. 19

    In Meteor, how do I know on the client-side when the server-side operation is done?

  20. 20

    How to Localize validation message (DataAnnotationsValidator) in blazor server side

  21. 21

    when I don't have access to the Server how can i close the socket Connection from client side?

  22. 22

    how do I access the object the object i sent to the server file

  23. 23

    how do I access the object the object i sent to the server file

  24. 24

    How to do unit test for HttpContext.Current.Server.MapPath

  25. 25

    How to do unit test for HttpContext.Current.Server.MapPath

  26. 26

    How do I get fake path for HttpContext.Current.Server.MapPath which is assigned to protected object inside method unit testing?

  27. 27

    How do I get fake path for HttpContext.Current.Server.MapPath which is assigned to protected object inside method unit testing?

  28. 28

    How can I access server-side variables via Meteor.call()?

  29. 29

    How to navigate to a blazor component from a razor page in a Blazor server-side app?

HotTag

Archive