'Why System.Web.HttpContext.Current is null in Task Run in asp.net mvc and How to have global variable per request?

I am using a structure to create a dictionary for each user request and set a value in it.

Now my question is how can I safe store the value of each request in the global variable?

Part of my code in Global.asax ...



        protected async Task Application_BeginRequestAsync(object sender, EventArgs e)
        {
            var app = (HttpApplication)sender;
            var ctx = app.Context;

            if (Request.Url.Host.StartsWith("www") && !Request.Url.IsLoopback)
            {
                UriBuilder builder = new UriBuilder(Request.Url);
                builder.Host = Request.Url.Host.Replace("www.", "");
                Response.Redirect(builder.ToString(), true);
            }
            var domain = Request.Url.Host + ((Request.Url.Port != 80 && Request.Url.Port != 443) ? ":" + Request.Url.Port.ToString() : "");
            domain = domain.Replace("www.", "");

            ctx.Items["ShopKey"] = domain;
            .
            .
            .
             etc code...

}

What variable is appropriate for this, and is the http context appropriate?

and another question i have use http context but task run variable is null, how do I fix the problem?

thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source