CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.11k stars 174 forks source link

IHttpContextAccessor currently doesn't work #268

Closed Dreamwalker666 closed 2 years ago

Dreamwalker666 commented 3 years ago

I discovered that I couldn't access the HttpContext in a DI based way. Not sure if this support was excluded for some reason (or I've completely gone about it the wrong way :)

I added the following before https://github.com/CarterCommunity/Carter/blob/master/src/Carter/CarterExtensions.cs#L86

                //  Setup HttpContext if services has been added with AddHttpContextAccessor
                var httpContext = (IHttpContextAccessor)ctx.RequestServices.GetService(typeof(IHttpContextAccessor));
                if (httpContext != null)
                {
                    httpContext.HttpContext = ctx;                    
                }

This solves my particular issue but would this be wanted in the main repo? If so I'll make a PR