locked
HttpContext in Custom Class RRS feed

  • Question

  • User1352387630 posted

    Hi All,

    Silly question....

    I'm trying to access HttpContextAccessor in a custom class (Helper Class for doing all sorts of global app stuff)... so this is what I've done:

    In Startup:

            public void ConfigureServices(IServiceCollection services)
            {
    
                services.AddHttpContextAccessor();
                ...
             }
    

    In my custom class:

     public class GlobalHelper
        {
            
            private readonly IHttpContextAccessor _accessor;
    
            public GlobalHelper(IHttpContextAccessor accessor)
            {
                _accessor = accessor;
            }
    
            public void stuff()
            {
                var context = _accessor.HttpContext;
    
               //more stuff
            }
    
         }

    So when I call my globalhelper class and voids...  I still have to create the instance like this (This is in a blazor view)

    //TOP OF PAGE
    @inject IHttpContextAccessor httpContextAccessor
    
    
    
    
    //In Code

    //Passing in the accessor from the inject into my blazor view MyBlazorApp.Classes.GlobalHelper ll = new MyBlazorApp.Classes.GlobalHelper(httpContextAccessor);
    ll.stuff();

    That's the way to do it.... or couldn't I just:

    MyBlazorApp.Classes.GlobalHelper ll = new MyBlazorApp.Classes.GlobalHelper(); 
    ll.stuff();


    If not... then how is this anything different than just passing around objects...?  As My global helper grows.... I'll probably be passing a heck of a lot of services into it...  Shouldn't it just be able to read from the accessor now without me passing it in every time I refer to it on a view?

    Tuesday, November 19, 2019 6:30 PM

Answers

  • User1352387630 posted

    I was able to get the answer from a colleague for anyone else battling with this.  For my specific scenario, I registered my global helper as a singleton.  httpcontextaccessor is passed into my global class which populates httpcontext, which I can then use in my functions.

    In Startup:

                services.AddSingleton<Classes.GlobalHelper>();
    

    In my view where I call my global helper at the top:

    @inject Classes.GlobalHelper _helper
    

    And now you can call your functions:

                _helper.DoStuff();
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 19, 2019 11:36 PM

All replies

  • User475983607 posted

    This does not sound like a good design.  For the most part, global application operations are handled in middleware.   There are times when you need to grab somehting from the context but I would pass the item (not the context) to the a class constructor from a Razor Page or an Action.  That makes unit testing a bit easier.

    Can you explain the problem this design solves? 

    Tuesday, November 19, 2019 7:40 PM
  • User1352387630 posted

    This is not the end design... I'm just trying to learn how to call context from a custom class first.  The end design will use specific objects from context.... however for now... Wanted to understand the right way calling the context, and if it possible to call context from a custom class without having to reference it from my views / controllers every time in my app.

    Tuesday, November 19, 2019 7:45 PM
  • User475983607 posted

    The Rudi

    This is not the end design... I'm just trying to learn how to call context from a custom class first.  The end design will use specific objects from context.... however for now... Wanted to understand the right way calling the context, and if it possible to call context from a custom class without having to reference it from my views / controllers every time in my app.

    ASP.NET Core uses DI.  

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.0

    Tuesday, November 19, 2019 7:59 PM
  • User1352387630 posted


    Thanks, but I already went through all the docs.  

    Tuesday, November 19, 2019 8:02 PM
  • User1352387630 posted

    I was able to get the answer from a colleague for anyone else battling with this.  For my specific scenario, I registered my global helper as a singleton.  httpcontextaccessor is passed into my global class which populates httpcontext, which I can then use in my functions.

    In Startup:

                services.AddSingleton<Classes.GlobalHelper>();
    

    In my view where I call my global helper at the top:

    @inject Classes.GlobalHelper _helper
    

    And now you can call your functions:

                _helper.DoStuff();
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 19, 2019 11:36 PM